ll | b52a49f | 2022-09-05 00:23:18 -0700 | [diff] [blame] | 1 | #include <stdio.h>
|
| 2 | #include <sys/types.h>
|
| 3 | #include <sys/socket.h>
|
| 4 | #include <arpa/inet.h>
|
| 5 | #include <string.h>
|
| 6 | #include <unistd.h>
|
| 7 | #include <binder/Parcel.h>
|
| 8 | #include <log/log.h>
|
| 9 | #include <cutils/jstring.h>
|
| 10 | #include <pthread.h>
|
| 11 | #include <sys/socket.h>
|
| 12 | #include <netinet/in.h>
|
| 13 | #include <arpa/inet.h>
|
| 14 | #include <errno.h>
|
| 15 | #include "lynq_timer.h"
|
| 16 |
|
| 17 | #define DEST_PORT 8088
|
| 18 | #define DSET_IP_ADDRESS "127.0.0.1"
|
| 19 | #define RIL_REQUEST_GET_IMSI 11
|
| 20 | #define MAX_LEN 1024
|
| 21 | #define TIMER 30
|
| 22 |
|
| 23 | using ::android::Parcel;
|
| 24 |
|
| 25 | static pthread_mutex_t g_lynq_sim_sendto_mutex;
|
| 26 |
|
| 27 | typedef struct{
|
| 28 | int uToken;
|
| 29 | int request;
|
| 30 | int paramLen;
|
| 31 | char param[MAX_LEN];
|
| 32 | }lynq_client_t;
|
| 33 |
|
| 34 | static int get_md_sta(void)
|
| 35 | {
|
| 36 | FILE *fp;
|
| 37 | char buffer[64]={};
|
| 38 | fp = popen("cat /sys/kernel/ccci/boot","r");
|
| 39 | if(fp == NULL)
|
| 40 | {
|
| 41 | RLOGD("function fp is null\n", __FUNCTION__);
|
| 42 | return 0;
|
| 43 | }
|
ll | 2f5e1c8 | 2022-09-07 00:01:43 -0700 | [diff] [blame^] | 44 | if(!strlen(buffer))
|
| 45 | {
|
| 46 | pclose(fp);
|
| 47 | return 0;
|
| 48 | }
|
ll | b52a49f | 2022-09-05 00:23:18 -0700 | [diff] [blame] | 49 | fgets(buffer,sizeof(buffer),fp);
|
| 50 | if(buffer[4] == '4')
|
| 51 | {
|
| 52 | pclose(fp);
|
| 53 | return 1;
|
| 54 | }
|
| 55 | pclose(fp);
|
| 56 | return 0;
|
| 57 | }
|
| 58 |
|
| 59 | static int check_service(const char *service)
|
| 60 | {
|
| 61 | FILE *fp;
|
| 62 | char buffer[1024]={};
|
| 63 | if(!strcmp(service, "/usr/sbin/mtkfusionrild"))
|
| 64 | {
|
| 65 | fp = popen("ps -ef|grep rild","r");
|
| 66 | }
|
| 67 | else if(!strcmp(service, "lynq-ril-service"))
|
| 68 | {
|
| 69 | fp = popen("ps -ef|grep ril-service","r");
|
| 70 | }
|
| 71 | if(fp == NULL)
|
| 72 | {
|
| 73 | RLOGD("function fp is null\n", __FUNCTION__);
|
| 74 | return 0;
|
| 75 | }
|
| 76 | while(NULL != fgets(buffer,sizeof(buffer),fp))
|
| 77 | {
|
| 78 | if(strstr(buffer,service))
|
| 79 | {
|
| 80 | pclose(fp);
|
| 81 | printf("check_service 1\n");
|
| 82 | return 1;
|
| 83 | }
|
| 84 | }
|
| 85 | printf("check_service 0\n");
|
| 86 | pclose(fp);
|
| 87 | return 0;
|
| 88 | }
|
| 89 |
|
| 90 | /**
|
| 91 | * @brief 30s request imei
|
| 92 | *
|
| 93 | * @param arg
|
| 94 | * @return void*
|
| 95 | */
|
| 96 | void * timer_request_imei(void * arg)
|
| 97 | {
|
| 98 | int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
| 99 | if (-1 == sock_fd)
|
| 100 | {
|
| 101 | RLOGD("__FUNCTION__ %s create sock_fd failed %s\n", __FUNCTION__, strerror(errno));
|
| 102 | /*set uci*/
|
| 103 | /*socket abnormal*/
|
| 104 | system("uci set lynq_uci.sdk_ready='3'");
|
| 105 | /*set uci*/
|
| 106 | return NULL;
|
| 107 | }
|
| 108 | struct timeval timeOut;
|
| 109 | timeOut.tv_sec = 3;//timeout time 3s
|
| 110 | timeOut.tv_usec = 0;
|
| 111 | if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeOut, sizeof(timeOut)) < 0)
|
| 112 | {
|
| 113 | RLOGD("__FUNCTION__ %s time out setting failed %s\n", __FUNCTION__, strerror(errno));
|
| 114 | /*set uci*/
|
| 115 | /*socket abnormal*/
|
| 116 | system("uci set lynq_uci.sdk_ready='3'");
|
| 117 | /*set uci*/
|
| 118 | return NULL;
|
| 119 | }
|
| 120 | struct sockaddr_in addr_serv;
|
| 121 | memset(&addr_serv, 0, sizeof(addr_serv));
|
| 122 | addr_serv.sin_family = AF_INET;
|
| 123 | addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS);
|
| 124 | addr_serv.sin_port = htons(DEST_PORT);
|
| 125 | int len_addr_serv = sizeof(addr_serv);
|
| 126 | int send_num = -1;
|
| 127 | int recv_num = -1;
|
| 128 | int resp_type = -1;
|
| 129 | int request = -1;
|
| 130 | int slot_id = -1;
|
| 131 | int res_error = -1;
|
| 132 | lynq_client_t client_t;
|
| 133 | char res_data[MAX_LEN] = {0};
|
| 134 | client_t.request = RIL_REQUEST_GET_IMSI;
|
| 135 | client_t.paramLen = 0;
|
| 136 | client_t.uToken = 999;
|
| 137 | memset(client_t.param, 0, sizeof(client_t.param));
|
| 138 | while (1)
|
| 139 | {
|
ll | b52a49f | 2022-09-05 00:23:18 -0700 | [diff] [blame] | 140 | bzero(res_data, MAX_LEN);
|
| 141 | pthread_mutex_lock(&g_lynq_sim_sendto_mutex);
|
| 142 | send_num = sendto(sock_fd, &client_t, sizeof(client_t), 0, (struct sockaddr *)&addr_serv, len_addr_serv);
|
| 143 | printf("send_num %d\n", send_num);
|
| 144 | if(send_num == 0)
|
| 145 | {
|
| 146 | RLOGD("__FUNCTION__ %s Close to end %s\n", __FUNCTION__, strerror(errno));
|
| 147 | //unknow
|
| 148 | system("uci set lynq_uci.sdk_ready='4'");
|
| 149 | pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
|
| 150 | continue;
|
| 151 | }
|
| 152 | if(send_num < 0)
|
| 153 | {
|
| 154 | RLOGD("__FUNCTION__ %s sendto error %s\n", __FUNCTION__, strerror(errno));
|
| 155 | //unknow
|
| 156 | system("uci set lynq_uci.sdk_ready='4'");
|
| 157 | pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
|
| 158 | continue;
|
| 159 | }
|
| 160 | //get data msg
|
| 161 | recv_num = recvfrom(sock_fd,res_data,sizeof(char)*MAX_LEN,0,(struct sockaddr *)&addr_serv,(socklen_t*)&len_addr_serv);
|
| 162 | printf("recv_num %d\n", recv_num);
|
| 163 | if(recv_num == 0)
|
| 164 | {
|
| 165 | RLOGD("__FUNCTION__ %s Close to end\n", __FUNCTION__);
|
| 166 | //unknow
|
| 167 | system("uci set lynq_uci.sdk_ready='4'");
|
| 168 | pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
|
| 169 | continue;;
|
| 170 | }
|
| 171 | if(recv_num == -1 && errno == EAGAIN)
|
| 172 | {
|
| 173 | RLOGD("__FUNCTION__ %srecvfrom fail because timeout\n", __FUNCTION__);
|
| 174 | /*set uci*/
|
| 175 | if(!get_md_sta())
|
| 176 | {
|
| 177 | system("uci set lynq_uci.sdk_ready='1'");
|
| 178 | }
|
| 179 | else
|
| 180 | {
|
| 181 | if(check_service("/usr/sbin/mtkfusionrild"))//rild work
|
| 182 | {
|
| 183 | if(!check_service("lynq-ril-service"))//not find,must be socket error
|
| 184 | {
|
| 185 | system("uci set lynq_uci.sdk_ready='3'");
|
| 186 | }
|
| 187 | else
|
| 188 | {
|
| 189 | //unknow error
|
| 190 | system("uci set lynq_uci.sdk_ready='4'");
|
| 191 | }
|
| 192 |
|
| 193 | }
|
| 194 | else//rild no work
|
| 195 | {
|
| 196 | //unknow
|
| 197 | system("uci set lynq_uci.sdk_ready='4'");
|
| 198 | }
|
| 199 | }
|
| 200 | pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
|
| 201 | continue;;
|
| 202 | }
|
| 203 | if(recv_num < 0)
|
| 204 | {
|
| 205 | RLOGD("__FUNCTION__ %srecvfrom fail %s\n", __FUNCTION__, strerror(errno));
|
| 206 | //unknow
|
| 207 | system("uci set lynq_uci.sdk_ready='4'");
|
| 208 | pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
|
| 209 | continue;;
|
| 210 | }
|
| 211 | Parcel p;
|
| 212 | p.setData((uint8_t *)res_data,sizeof(char)*recv_num);
|
| 213 | p.setDataPosition(0);
|
| 214 | if(p.dataAvail() > 0)
|
| 215 | {
|
| 216 | p.readInt32(&resp_type);
|
| 217 | p.readInt32(&request);
|
| 218 | p.readInt32(&slot_id);
|
| 219 | p.readInt32(&res_error);
|
| 220 | }
|
| 221 | /*judge the res_error*/
|
| 222 | if(!res_error)
|
| 223 | {
|
| 224 | system("uci set lynq_uci.sdk_ready='2'");//Md configurations are inconsistent with AP configurations
|
| 225 | }
|
| 226 | else
|
| 227 | {
|
| 228 | system("uci set lynq_uci.sdk_ready='0'");
|
| 229 | }
|
| 230 | RLOGD("__FUNCTION__ %s res_error %d\n", __FUNCTION__, res_error);
|
| 231 | /*judge the res_error*/
|
| 232 | pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
|
ll | 2f5e1c8 | 2022-09-07 00:01:43 -0700 | [diff] [blame^] | 233 | sleep(TIMER);
|
ll | b52a49f | 2022-09-05 00:23:18 -0700 | [diff] [blame] | 234 | }
|
| 235 | return NULL;
|
| 236 | }
|
| 237 |
|
| 238 |
|
| 239 | void start_timer_request(void)
|
| 240 | {
|
| 241 | pthread_t thid;
|
| 242 | pthread_attr_t a;
|
| 243 | pthread_attr_init(&a);
|
| 244 | pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
|
| 245 | int ret = pthread_create(&thid, &a, timer_request_imei, NULL);
|
| 246 | if(ret != 0){
|
| 247 | RLOGD("pthread_create error!!!");
|
| 248 | return;
|
| 249 | }
|
| 250 | return;
|
| 251 | }
|
| 252 |
|