android
2024-07-25 2dcf9cc893a27c50d538baf8b5d47ff06fe495d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <android/log.h>
#include <fcntl.h>
 
#define TAG "CanControl"
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)
 
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_serial_CanControl_sendMessage(JNIEnv *env, jclass clazz, jstring id, jstring dlc,
                                               jstring data, jstring ifname) {
    // TODO: implement sendMessage()
    int                  fd;
    int                  i;
    struct sockaddr_can  addr;
    struct ifreq         ifr;
    struct can_frame     frame;
    unsigned int         byte;
    char                *token;
 
    const char *sendId = env->GetStringUTFChars(id, 0);
    const char *sendDlc = env->GetStringUTFChars(dlc, 0);
    const char *sendMsg = env->GetStringUTFChars(data, 0);
    const char *if_name = env->GetStringUTFChars(ifname, 0);
    if ( sendId == nullptr || sendDlc == nullptr || sendMsg == nullptr || if_name == nullptr)
    {
        LOGE("Invalid parameter");
        return -1;
    }
 
    // create socket
    fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if(fd < 0)
    {
        LOGE("create can socket failed:%s\n", strerror(errno));
        return -2;
    }
 
    strcpy(ifr.ifr_name, if_name);
    ioctl(fd, SIOCGIFINDEX, &ifr);
    addr.can_family = AF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;
 
    if (bind(fd, (struct sockaddr*)&addr, sizeof (addr)) < 0)
    {
        LOGE("bind socket failed:%s\n", strerror(errno));
        return -3;
    }
    sscanf(sendId, "%X", &byte);
    frame.can_id = byte;
    sscanf(sendDlc, "%u", &byte);
    frame.can_dlc = byte;
    token = strtok((char *)sendMsg, " ");
    for (int i = 0; i < frame.can_dlc && token != NULL; ++i)
    {
        unsigned int data_byte;
        sscanf(token, "%02X", &data_byte);
        frame.data[i] = (unsigned char)data_byte;
        token = strtok(NULL, " ");
    }
 
    if (write(fd, &frame, sizeof (struct can_frame)) != sizeof (struct can_frame))
    {
        LOGE("write data failed:%s\n", strerror(errno));
        return -4;
    }
 
    LOGD("Send CAN frame: ID=0x%X DLC=%d data=", frame.can_id, frame.can_dlc);
    for(int i = 0; i < frame.can_dlc; i++)
    {
        LOGD("%02X ", frame.data[i]);
    }
    printf("\n");
 
    close(fd);
    env->ReleaseStringUTFChars(id, sendId);
    env->ReleaseStringUTFChars(dlc, sendDlc);
    env->ReleaseStringUTFChars(data, sendMsg);
    env->ReleaseStringUTFChars(ifname, if_name);
    return 0;
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_serial_CanControl_receiveMessage(JNIEnv *env, jclass clazz, jstring ifname) {
    // TODO: implement receiveMessage()
    int                  fd;
    int                  rv;
    struct sockaddr_can  addr;
    struct ifreq         ifr;
    struct can_frame     frame;
    char                 recvMsg[128];
    char                 buf[4];
 
    const char *if_name = env->GetStringUTFChars(ifname, 0);
    if (if_name == nullptr)
    {
        LOGE("Invalid parameter");
        return nullptr;
    }
 
    fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if(fd < 0)
    {
        LOGE("create can socket failed:%s\n", strerror(errno));
        return nullptr;
    }
    strcpy(ifr.ifr_name, if_name);
    ioctl(fd, SIOCGIFINDEX, &ifr);
    addr.can_family = AF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;
 
    if (bind(fd, (struct sockaddr*)&addr, sizeof (addr)) < 0)
    {
        LOGE("bind socket failed:%s\n", strerror(errno));
        return nullptr;
    }
 
    rv = read(fd, &frame, sizeof (struct can_frame));
    if (rv < 0)
    {
        LOGE("read data failed:%s\n", strerror(errno));
        return nullptr;
    } else if (rv < sizeof (struct can_frame))
    {
        LOGE("read incomplete CAN frame\n");
        return nullptr;
    }
    memset(recvMsg, 0, sizeof (recvMsg));
    sprintf(recvMsg, "ID=%X DLC=%d Data=", frame.can_id, frame.can_dlc);
    for (int i = 0; i < frame.can_dlc; ++i) {
        memset(buf, 0, sizeof (buf));
        sprintf(buf, "%02X ", frame.data[i]);
        strcat(recvMsg, buf);
    }
    LOGD("recived: %s", recvMsg);
    close(fd);
    env->ReleaseStringUTFChars(ifname, if_name);
    return env->NewStringUTF(recvMsg);
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_serial_CanControl_openCan(JNIEnv *env, jclass clazz, jstring ifname,
                                           jstring bitrate) {
    // TODO: implement openCan()
    char *ifname_native;
    char *bitrate_native;
    char  command[128];
 
    ifname_native = (char *)env->GetStringUTFChars(ifname, 0);
    bitrate_native = (char *)env->GetStringUTFChars(bitrate, 0);
 
    memset(command, 0, sizeof (command));
    snprintf(command, sizeof (command), "ip link set %s down", ifname_native);
    if( system(command) == -1)
    {
        LOGE("system %s failure:%s\n", command, strerror(errno));
        return -1;
    }
    LOGD("system %s success\n", command);
 
    memset(command, 0, sizeof (command));
    snprintf(command, sizeof(command), "ip link set %s up type can bitrate %d", ifname_native, bitrate_native);
    if( system(command) == -1)
    {
        LOGE("system %s failure:%s\n", command, strerror(errno));
        return -1;
    }
    LOGD("system %s success\n", command);
 
    memset(command, 0, sizeof (command));
    snprintf(command, sizeof(command), "ip link set %s up type can", ifname_native);
    if( system(command) == -1)
    {
        LOGE("system %s failure:%s\n", command, strerror(errno));
        return -1;
    }
    LOGD("system %s success\n", command);
    env->ReleaseStringUTFChars(ifname, ifname_native);
    env->ReleaseStringUTFChars(bitrate, bitrate_native);
    return 0;
}