| lh | ee0d3b3 | 2024-04-25 03:54:48 -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 <list> |
| 12 | #include <vendor-ril/telephony/ril.h> |
| 13 | #include <vendor-ril/telephony/mtk_ril_sp.h> |
| 14 | #include "liblog/lynq_deflog.h" |
| 15 | #include "lynq_sim_urc.h" |
| 16 | |
| 17 | int module_len_urc_addr_serv; |
| 18 | struct sockaddr_in module_urc_addr_serv; |
| 19 | static int module_urc_sock_fd = -1; |
| 20 | int module_urc_status = 1; |
| 21 | pthread_t module_urc_tid = -1; |
| 22 | |
| 23 | static pthread_mutex_t s_ProcessUrcMsgBlockMutex = PTHREAD_MUTEX_INITIALIZER; |
| 24 | #define BLOCK_PROCESS_URC_MSG_INIT() pthread_mutex_init(&s_ProcessUrcMsgBlockMutex,NULL) |
| 25 | #define BLOCK_PROCESS_URC_MSG_LOCK() pthread_mutex_lock(&s_ProcessUrcMsgBlockMutex) |
| 26 | #define BLOCK_PROCESS_URC_MSG_UNLOCK() pthread_mutex_unlock(&s_ProcessUrcMsgBlockMutex) |
| 27 | |
| 28 | bool is_support_urc(int urc_id) |
| 29 | { |
| 30 | switch(urc_id) |
| 31 | { |
| 32 | case LYNQ_URC_ALLOW_DATA: |
| 33 | return true; |
| 34 | default: |
| 35 | return false; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void *thread_urc_recv(void *p) |
| 40 | { |
| 41 | Parcel *urc_p =NULL; |
| 42 | char urc_data[LYNQ_REC_BUF]; |
| 43 | int res = 0; |
| 44 | lynq_head_t* phead; |
| 45 | LYINFLOG("urc recv thread is running"); |
| 46 | while(module_urc_status) |
| 47 | { |
| 48 | bzero(urc_data,LYNQ_REC_BUF); |
| 49 | res = recvfrom(module_urc_sock_fd,urc_data,sizeof(urc_data),0,(struct sockaddr *)&module_urc_addr_serv,(socklen_t*)&module_len_urc_addr_serv); |
| 50 | if(res<sizeof(int32_t)*2) |
| 51 | { |
| 52 | LYERRLOG("thread_urc_recv step2 fail: res is %d",res); |
| 53 | continue; |
| 54 | } |
| 55 | |
| 56 | phead=(lynq_head_t*) urc_data; |
| 57 | if(is_support_urc(phead->urcid)==false) |
| 58 | { |
| 59 | continue; |
| 60 | } |
| 61 | urc_p = new Parcel(); |
| 62 | if(urc_p == NULL) |
| 63 | { |
| 64 | LYERRLOG("new parcel failure!!!"); |
| 65 | continue; |
| 66 | } |
| 67 | urc_p->setData((uint8_t *)urc_data,res); // p.setData((uint8_t *) buffer, buflen); |
| 68 | urc_p->setDataPosition(0); |
| 69 | if(urc_p->dataAvail()>0) |
| 70 | { |
| 71 | urc_msg_process(urc_p); |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | delete urc_p; |
| 76 | urc_p = NULL; |
| 77 | } |
| 78 | } |
| 79 | LYINFLOG("urc recv thread ended"); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | void lynq_close_urc_rev_thread() |
| 84 | { |
| 85 | int ret; |
| 86 | |
| 87 | BLOCK_PROCESS_URC_MSG_LOCK(); //just cancel urc process tid when recv from |
| 88 | module_urc_status = 0; |
| 89 | if(module_urc_tid!=-1) |
| 90 | { |
| 91 | ret = pthread_cancel(module_urc_tid); |
| 92 | LYINFLOG("pthread cancel urc rev ret = %d",ret); |
| 93 | } |
| 94 | BLOCK_PROCESS_URC_MSG_UNLOCK(); |
| 95 | if(module_urc_tid != -1) |
| 96 | { |
| 97 | ret = pthread_join(module_urc_tid,NULL); |
| 98 | LYINFLOG("pthread join urc tid ret = %d",ret); |
| 99 | module_urc_tid =-1; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void lynq_close_urc_socket() |
| 104 | { |
| 105 | if (module_urc_sock_fd >= 0) |
| 106 | { |
| 107 | close(module_urc_sock_fd); |
| 108 | module_urc_sock_fd =-1; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | int lynq_setup_urc_socket() |
| 113 | { |
| 114 | int on = 1; |
| 115 | int ret = 0; |
| 116 | module_len_urc_addr_serv = sizeof(sockaddr_in); |
| 117 | module_urc_sock_fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 118 | if (module_urc_sock_fd <0){ |
| 119 | LYERRLOG("urc socket error"); |
| 120 | return RESULT_ERROR; |
| 121 | } |
| 122 | module_urc_addr_serv.sin_family = AF_INET; |
| 123 | module_urc_addr_serv.sin_port = htons(LYNQ_URC_SERVICE_PORT); |
| 124 | module_urc_addr_serv.sin_addr.s_addr = inet_addr(LYNQ_URC_ADDRESS); |
| 125 | /* Set socket to allow reuse of address and port, SO_REUSEADDR value is 2*/ |
| 126 | ret = setsockopt(module_urc_sock_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on); |
| 127 | if(ret <0) |
| 128 | { |
| 129 | LYERRLOG("urc socket set error"); |
| 130 | close(module_urc_sock_fd); |
| 131 | module_urc_sock_fd =-1; |
| 132 | return RESULT_ERROR; |
| 133 | } |
| 134 | ret = bind(module_urc_sock_fd ,(struct sockaddr*)&module_urc_addr_serv, sizeof(module_urc_addr_serv)); |
| 135 | if(ret <0) |
| 136 | { |
| 137 | LYERRLOG("urc socket bind error"); |
| 138 | close(module_urc_sock_fd); |
| 139 | module_urc_sock_fd =-1; |
| 140 | return RESULT_ERROR; |
| 141 | } |
| 142 | return RESULT_OK; |
| 143 | } |
| 144 | |
| 145 | int lynq_start_all_urc_socket_thread() |
| 146 | { |
| 147 | int ret= lynq_setup_urc_socket(); |
| 148 | if(ret!=RESULT_OK) |
| 149 | { |
| 150 | LYERRLOG("call lynq_setup_urc_socket fail"); |
| 151 | return RESULT_ERROR; |
| 152 | } |
| 153 | |
| 154 | module_urc_status = 1; |
| 155 | ret = pthread_create(&module_urc_tid,NULL,thread_urc_recv,NULL); |
| 156 | if(ret <0) |
| 157 | { |
| 158 | LYERRLOG("urc recv pthread create error"); |
| 159 | module_urc_status = 0; |
| 160 | lynq_close_urc_socket(); |
| 161 | return RESULT_ERROR; |
| 162 | } |
| 163 | LYINFLOG("urc start success"); |
| 164 | return RESULT_OK; |
| 165 | } |
| 166 | |
| 167 | void lynq_close_all_urc_socket_thread() |
| 168 | { |
| 169 | lynq_close_urc_rev_thread(); |
| 170 | lynq_close_urc_socket(); |
| 171 | LYERRLOG("close all urc socket thread!!!"); |
| 172 | } |
| 173 | |