rjw | 7ee7bb4 | 2023-01-18 11:34:28 +0800 | [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 <list> |
| 12 | #include "lynq_data.h" |
| 13 | #include "lynq_data_urc.h" |
| 14 | #include "liblog/lynq_deflog.h" |
| 15 | #include "lynq_shm.h" |
| 16 | |
| 17 | #define LYNQ_REC_BUF 8192 |
| 18 | |
| 19 | static std::list<Parcel*> s_urc_recv_parcel_list; |
| 20 | |
| 21 | int lynq_len_urc_addr_serv; |
| 22 | struct sockaddr_in urc_local_addr; |
| 23 | static int lynq_urc_sockfd = -1; |
| 24 | bool data_urc_recive_status = 1; |
| 25 | static pthread_mutex_t s_lynq_urc_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 26 | static pthread_cond_t s_lynq_urc_cond = PTHREAD_COND_INITIALIZER; |
| 27 | |
| 28 | /*recv*/ |
| 29 | pthread_t data_urc_recv_tid = -1; |
| 30 | static pthread_mutex_t s_lynq_urc_recv_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 31 | static pthread_cond_t s_lynq_urc_recv_cond = PTHREAD_COND_INITIALIZER; |
| 32 | |
| 33 | /*process*/ |
| 34 | pthread_t data_urc_process_tid = -1; |
| 35 | bool data_urc_process_status = 1; |
| 36 | static pthread_mutex_t s_lynq_urc_process_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 37 | static pthread_cond_t s_lynq_urc_process_cond = PTHREAD_COND_INITIALIZER; |
| 38 | |
| 39 | #define SHM_BUFFER_INDEX_OFFSET 1 |
| 40 | #define SHM_BUFFER_SIZE_OFFSET 16 |
| 41 | #define SHM_BUFFER_INDEX_MASK 0x0000007F |
| 42 | #define SHM_BUFFER_SIZE_MASK 0x0000FFFF |
| 43 | |
| 44 | bool urc_data_is_in_shm_data(int responseType,int& level, int& index, int& size) |
| 45 | { |
| 46 | int shm_index=((responseType>>SHM_BUFFER_INDEX_OFFSET)&SHM_BUFFER_INDEX_MASK); |
| 47 | if (shm_index>0) |
| 48 | { |
| 49 | index=shm_index-1; |
| 50 | size=((responseType>>SHM_BUFFER_SIZE_OFFSET)&SHM_BUFFER_SIZE_MASK); |
| 51 | if(size>=sizeof(int32_t)*3 && get_shem_buffer_level(size,&level)) |
| 52 | { |
| 53 | LYINFLOG("urc_data_is_in_shm_data level is %d, index is %d size is %d",level,index,size); |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | LYINFLOG("urc_data_is_in_shm_data return false, responseType is %d",responseType); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | void cleanup_urc_process_mutex(void *arg) |
| 62 | { |
| 63 | pthread_mutex_unlock(&s_lynq_urc_process_mutex); |
| 64 | } |
| 65 | |
| 66 | void *thread_urc_recv() |
| 67 | { |
| 68 | Parcel *urc_p =NULL; |
| 69 | char urc_data[LYNQ_REC_BUF]; |
| 70 | int res = 0; |
| 71 | lynq_head_t* phead; |
rjw | d2fe098 | 2023-02-15 15:02:00 +0800 | [diff] [blame] | 72 | int level,index,size; |
| 73 | uint8_t * shm_buffer; |
| 74 | |
rjw | 7ee7bb4 | 2023-01-18 11:34:28 +0800 | [diff] [blame] | 75 | LYINFLOG("urc recv thread is running"); |
| 76 | while(data_urc_recive_status) |
| 77 | { |
| 78 | bzero(urc_data,LYNQ_REC_BUF); |
lh | 0e551da | 2023-03-28 09:50:20 +0800 | [diff] [blame] | 79 | res = recvfrom(lynq_urc_sockfd,urc_data,sizeof(urc_data),0,(struct sockaddr *)&urc_local_addr,(socklen_t*)&lynq_len_urc_addr_serv); |
rjw | 7ee7bb4 | 2023-01-18 11:34:28 +0800 | [diff] [blame] | 80 | if(res<sizeof(int32_t)*2) |
| 81 | { |
| 82 | LYERRLOG("thread_urc_recv step2 fail: res is %d",res); |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | phead=(lynq_head_t*) urc_data; |
| 87 | if(is_support_urc(phead->urcid)==false) |
| 88 | { |
| 89 | continue; |
| 90 | } |
| 91 | urc_p = new Parcel(); |
| 92 | if(urc_p == NULL) |
| 93 | { |
| 94 | LYERRLOG("new parcel failure!!!"); |
| 95 | continue; |
| 96 | } |
rjw | 7ee7bb4 | 2023-01-18 11:34:28 +0800 | [diff] [blame] | 97 | if(urc_data_is_in_shm_data(phead->resp_type,level,index,size)) |
| 98 | { |
rjw | d2fe098 | 2023-02-15 15:02:00 +0800 | [diff] [blame] | 99 | shm_buffer = (uint8_t *)get_shem_buffer(level,index); // p.setData((uint8_t *) buffer, buflen); |
| 100 | LYINFLOG("shm pointer is %p", shm_buffer); |
| 101 | urc_p->setData(shm_buffer,size); |
rjw | 7ee7bb4 | 2023-01-18 11:34:28 +0800 | [diff] [blame] | 102 | } |
| 103 | else if(res>=sizeof(int32_t)*3) |
| 104 | { |
| 105 | urc_p->setData((uint8_t *)urc_data,res); // p.setData((uint8_t *) buffer, buflen); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | LYERRLOG("res %d error!!!", res); |
| 110 | delete urc_p; |
| 111 | urc_p = NULL; |
| 112 | continue; |
| 113 | } |
| 114 | urc_p->setDataPosition(0); |
| 115 | if(urc_p->dataAvail()>0) |
| 116 | { |
| 117 | pthread_mutex_lock(&s_lynq_urc_process_mutex); |
| 118 | s_urc_recv_parcel_list.push_back(urc_p); |
| 119 | pthread_cond_broadcast(&s_lynq_urc_process_cond); |
| 120 | pthread_mutex_unlock(&s_lynq_urc_process_mutex); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | delete urc_p; |
| 125 | urc_p = NULL; |
| 126 | } |
| 127 | } |
| 128 | LYINFLOG("urc recv thread ended"); |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | void *thread_urc_process() |
| 133 | { |
| 134 | Parcel *urc_p =NULL; |
| 135 | std::list<Parcel*>::iterator iter; |
| 136 | |
| 137 | LYINFLOG("urc process thread is running"); |
| 138 | pthread_cleanup_push(cleanup_urc_process_mutex, NULL); // thread cleanup handler |
| 139 | while (data_urc_process_status) |
| 140 | { |
| 141 | pthread_mutex_lock(&s_lynq_urc_process_mutex); |
| 142 | while(s_urc_recv_parcel_list.empty()) |
| 143 | { |
| 144 | pthread_cond_wait(&s_lynq_urc_process_cond,&s_lynq_urc_process_mutex); |
| 145 | } |
| 146 | iter=s_urc_recv_parcel_list.begin(); |
| 147 | urc_p = (*iter); |
| 148 | s_urc_recv_parcel_list.erase(iter); |
| 149 | pthread_mutex_unlock(&s_lynq_urc_process_mutex); |
| 150 | urc_p->setDataPosition(0); |
| 151 | if(urc_p->dataAvail()>0) |
| 152 | { |
| 153 | pthread_mutex_lock(&s_lynq_urc_mutex); |
| 154 | urc_msg_process(urc_p); |
| 155 | pthread_mutex_unlock(&s_lynq_urc_mutex); |
| 156 | } |
| 157 | delete urc_p; |
| 158 | urc_p = NULL; |
| 159 | } |
| 160 | pthread_cleanup_pop(0); |
| 161 | LYINFLOG("urc process thread ended"); |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | int lynq_socket_recv_start() |
| 166 | { |
lh | 0e551da | 2023-03-28 09:50:20 +0800 | [diff] [blame] | 167 | int rt=0; |
| 168 | int on=1; |
| 169 | struct sockaddr_in urc_local_addr; |
| 170 | pthread_attr_t attr; |
| 171 | lynq_urc_sockfd = socket(AF_INET,SOCK_DGRAM,0); |
Hong_Liu | 6149f18 | 2023-05-12 02:15:14 -0700 | [diff] [blame] | 172 | lynq_len_urc_addr_serv = sizeof(urc_local_addr); |
lh | 0e551da | 2023-03-28 09:50:20 +0800 | [diff] [blame] | 173 | if(lynq_urc_sockfd < 0) |
| 174 | { |
| 175 | LYERRLOG("create socket for udp fail"); |
| 176 | return -1; |
| 177 | } |
| 178 | urc_local_addr.sin_family = AF_INET; |
| 179 | urc_local_addr.sin_port = htons(LYNQ_URC_SERVICE_PORT); |
lh | 2974e4a | 2024-02-21 00:14:24 -0800 | [diff] [blame] | 180 | urc_local_addr.sin_addr.s_addr = inet_addr(LYNQ_RIL_FWK_BROADCAST_IP);/*hong.liu change broadcast addr on 2024.2.18*/ |
lh | 0e551da | 2023-03-28 09:50:20 +0800 | [diff] [blame] | 181 | /* Set socket to allow reuse of address and port, SO_REUSEADDR value is 2*/ |
| 182 | rt = setsockopt(lynq_urc_sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on); |
| 183 | if(rt<0) |
| 184 | { |
| 185 | LYERRLOG("SO_REUSEADDR fail"); |
| 186 | close(lynq_urc_sockfd); |
| 187 | lynq_urc_sockfd = -1; |
| 188 | return -1; |
| 189 | } |
| 190 | rt = bind(lynq_urc_sockfd ,(struct sockaddr*)&urc_local_addr, sizeof(urc_local_addr)); |
| 191 | if (rt == -1) |
| 192 | { |
| 193 | LYERRLOG("bind failed"); |
| 194 | close(lynq_urc_sockfd); |
| 195 | lynq_urc_sockfd = -1; |
| 196 | return -1; |
| 197 | } |
| 198 | return 0; |
rjw | 7ee7bb4 | 2023-01-18 11:34:28 +0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | int lynq_socket_recv_stop() |
| 202 | { |
| 203 | if (lynq_urc_sockfd >=0) |
| 204 | { |
| 205 | close(lynq_urc_sockfd); |
| 206 | lynq_urc_sockfd = -1; |
| 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | void lynq_urc_recv_thread_stop() |
| 212 | { |
| 213 | int ret; |
| 214 | |
| 215 | pthread_mutex_lock(&s_lynq_urc_process_mutex); |
| 216 | data_urc_recive_status = 0; |
| 217 | if (data_urc_recv_tid != -1) |
| 218 | { |
| 219 | ret = pthread_cancel(data_urc_recv_tid); |
| 220 | LYDBGLOG("pthread cancel ret = %d",ret); |
| 221 | } |
| 222 | pthread_mutex_unlock(&s_lynq_urc_process_mutex); |
| 223 | |
| 224 | if (data_urc_recv_tid != -1) |
| 225 | { |
| 226 | ret = pthread_join(data_urc_recv_tid,NULL); |
| 227 | LYDBGLOG("pthread join ret = %d",ret); |
| 228 | data_urc_recv_tid = -1; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void lynq_urc_process_thread_stop() |
| 233 | { |
| 234 | int ret; |
| 235 | |
| 236 | pthread_mutex_lock(&s_lynq_urc_process_mutex); |
| 237 | pthread_mutex_lock(&s_lynq_urc_mutex); |
| 238 | |
| 239 | data_urc_process_status = 0; |
| 240 | if (data_urc_process_tid != -1) |
| 241 | { |
| 242 | ret = pthread_cancel(data_urc_process_tid); |
| 243 | LYDBGLOG("pthread cancel ret = %d",ret); |
| 244 | } |
| 245 | pthread_mutex_unlock(&s_lynq_urc_mutex); |
| 246 | pthread_mutex_unlock(&s_lynq_urc_process_mutex); |
| 247 | |
| 248 | if (data_urc_process_tid != -1) |
| 249 | { |
| 250 | ret = pthread_join(data_urc_process_tid,NULL); |
| 251 | LYDBGLOG("pthread join ret = %d",ret); |
| 252 | data_urc_process_tid = -1; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | |
| 257 | int lynq_init_data_urc_thread() |
| 258 | { |
| 259 | int ret = 0; |
| 260 | if(ril_init_mem()!=0) |
| 261 | { |
| 262 | LYERRLOG("ril_init_mem fail"); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | ret = lynq_socket_recv_start(); |
| 267 | if (ret != 0) |
| 268 | { |
| 269 | LYERRLOG("lynq_socket_recv_start fail"); |
| 270 | ril_deinit_mem(); |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | pthread_mutex_init(&s_lynq_urc_process_mutex,NULL); |
| 275 | pthread_mutex_init(&s_lynq_urc_recv_mutex,NULL); |
| 276 | |
| 277 | pthread_mutex_lock(&s_lynq_urc_process_mutex); |
| 278 | std::list<Parcel*>::iterator iter; |
| 279 | for (iter=s_urc_recv_parcel_list.begin();iter!=s_urc_recv_parcel_list.end();++iter) |
| 280 | { |
| 281 | delete(*iter); |
| 282 | } |
| 283 | s_urc_recv_parcel_list.clear(); |
| 284 | pthread_mutex_unlock(&s_lynq_urc_process_mutex); |
| 285 | |
| 286 | pthread_mutex_init(&s_lynq_urc_mutex,NULL); |
| 287 | data_urc_recive_status = 1; |
| 288 | |
| 289 | ret = pthread_create(&data_urc_recv_tid,NULL,thread_urc_recv,NULL); |
| 290 | if (ret < 0) |
| 291 | { |
| 292 | LYERRLOG("urc recv pthread create error"); |
| 293 | data_urc_recive_status = 0; |
| 294 | lynq_socket_recv_stop(); |
| 295 | ril_deinit_mem(); |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | data_urc_process_status = 1; |
| 300 | ret = pthread_create(&data_urc_process_tid,NULL,thread_urc_process,NULL); |
| 301 | if (ret < 0) |
| 302 | { |
| 303 | LYERRLOG("urc recv pthread create error"); |
| 304 | |
| 305 | data_urc_process_status = 0; |
| 306 | lynq_socket_recv_stop(); |
| 307 | lynq_urc_recv_thread_stop(); |
| 308 | |
| 309 | return -1; |
| 310 | } |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | int lynq_deinit_data_urc_thread() |
| 315 | { |
| 316 | lynq_socket_recv_stop(); |
| 317 | lynq_urc_recv_thread_stop(); |
| 318 | lynq_urc_process_thread_stop(); |
| 319 | |
| 320 | pthread_mutex_lock(&s_lynq_urc_process_mutex); |
| 321 | std::list<Parcel*>::iterator iter; |
| 322 | for (iter=s_urc_recv_parcel_list.begin();iter!=s_urc_recv_parcel_list.end();++iter) |
| 323 | { |
| 324 | delete(*iter); |
| 325 | } |
| 326 | s_urc_recv_parcel_list.clear(); |
| 327 | pthread_mutex_unlock(&s_lynq_urc_process_mutex); |
| 328 | |
| 329 | ril_deinit_mem(); |
rjw | eb65a2f | 2023-02-01 16:43:23 +0800 | [diff] [blame] | 330 | return 0; |
| 331 | } |