b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 1 | #include <sys/types.h>
|
| 2 | #include <sys/socket.h>
|
| 3 | #include <sys/un.h>
|
| 4 | #include <unistd.h>
|
| 5 | #include <arpa/inet.h>
|
| 6 | #include <stdio.h>
|
| 7 | #include <stdlib.h>
|
| 8 | #include <signal.h>
|
| 9 | #include <string.h>
|
| 10 | #include <pthread.h>
|
| 11 | #include <fcntl.h>
|
| 12 | #include <errno.h>
|
| 13 | #include <stdint.h>
|
| 14 | #include <dlfcn.h>
|
| 15 | #include <stdbool.h>
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 16 | #include "gsw_at_interface.h"
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 17 | #ifndef LOG_ERR_LEVEL
|
| 18 | #define LOG_ERR_LEVEL 3 /* error conditions */
|
| 19 | #endif
|
| 20 | #ifndef LOG_WARN_LEVEL
|
| 21 | #define LOG_WARN_LEVEL 4 /* warning conditions */
|
| 22 | #endif
|
| 23 | #ifndef LOG_INFO_LEVEL
|
| 24 | #define LOG_INFO_LEVEL 6 /* informational */
|
| 25 | #endif
|
| 26 | #ifndef LOG_DEBUG_LEVEL
|
| 27 | #define LOG_DEBUG_LEVEL 7 /* debug-level messages */
|
| 28 | #endif
|
| 29 | #ifndef LOG_VERBOSE_LEVEL
|
| 30 | #define LOG_VERBOSE_LEVEL 8
|
| 31 | #endif
|
| 32 |
|
l.yang | 6a42e4d | 2025-05-28 01:04:20 -0700 | [diff] [blame] | 33 | #define GSW_AT "[HAL][GSW_AT]"
|
| 34 |
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 35 | #define LOGV(fmt, args ...) \
|
| 36 | do{ \
|
| 37 | char *file_ptr_1001 = __FILE__; \
|
| 38 | char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
|
| 39 | char line_1001[10] = {0}; \
|
| 40 | sprintf(line_1001, "%d", __LINE__); \
|
| 41 | while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
|
| 42 | if(*ptr_1001 == '/') \
|
| 43 | break; \
|
| 44 | ptr_1001--; \
|
| 45 | } \
|
l.yang | 6a42e4d | 2025-05-28 01:04:20 -0700 | [diff] [blame] | 46 | fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: "GSW_AT"" fmt, ptr_1001 + 1, line_1001, ##args); \
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 47 | } while(0)
|
| 48 |
|
| 49 | #define LOGI(fmt, args...) \
|
| 50 | do{ \
|
| 51 | char *file_ptr_1001 = __FILE__; \
|
| 52 | char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
|
| 53 | char line_1001[10] = {0}; \
|
| 54 | sprintf(line_1001, "%d", __LINE__); \
|
| 55 | while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
|
| 56 | if(*ptr_1001 == '/') \
|
| 57 | break; \
|
| 58 | ptr_1001--; \
|
| 59 | } \
|
l.yang | 6a42e4d | 2025-05-28 01:04:20 -0700 | [diff] [blame] | 60 | fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: "GSW_AT"" fmt, ptr_1001 + 1, line_1001, ##args); \
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 61 | } while(0)
|
| 62 |
|
| 63 | #define LOGD(fmt, args...) \
|
| 64 | do{ \
|
| 65 | char *file_ptr_1001 = __FILE__; \
|
| 66 | char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
|
| 67 | char line_1001[10] = {0}; \
|
| 68 | sprintf(line_1001, "%d", __LINE__); \
|
| 69 | while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
|
| 70 | if(*ptr_1001 == '/') \
|
| 71 | break; \
|
| 72 | ptr_1001--; \
|
| 73 | } \
|
l.yang | 6a42e4d | 2025-05-28 01:04:20 -0700 | [diff] [blame] | 74 | fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: "GSW_AT"" fmt, ptr_1001 + 1, line_1001, ##args); \
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 75 | } while(0)
|
| 76 |
|
| 77 | #define LOGW(fmt, args...) \
|
| 78 | do{ \
|
| 79 | char *file_ptr_1001 = __FILE__; \
|
| 80 | char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
|
| 81 | char line_1001[10] = {0}; \
|
| 82 | sprintf(line_1001, "%d", __LINE__); \
|
| 83 | while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
|
| 84 | if(*ptr_1001 == '/') \
|
| 85 | break; \
|
| 86 | ptr_1001--; \
|
| 87 | } \
|
l.yang | 6a42e4d | 2025-05-28 01:04:20 -0700 | [diff] [blame] | 88 | fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: "GSW_AT"" fmt, ptr_1001 + 1, line_1001, ##args); \
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 89 | } while(0)
|
| 90 |
|
| 91 | #define LOGE(fmt, args...) \
|
| 92 | do{ \
|
| 93 | char *file_ptr_1001 = __FILE__; \
|
| 94 | char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
|
| 95 | char line_1001[10] = {0}; \
|
| 96 | sprintf(line_1001, "%d", __LINE__); \
|
| 97 | while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
|
| 98 | if(*ptr_1001 == '/') \
|
| 99 | break; \
|
| 100 | ptr_1001--; \
|
| 101 | } \
|
l.yang | 6a42e4d | 2025-05-28 01:04:20 -0700 | [diff] [blame] | 102 | fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: "GSW_AT"" fmt, ptr_1001 + 1, line_1001, ##args); \
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 103 | } while(0)
|
| 104 |
|
| 105 |
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 106 | #define GSW_HAL_MEM_INVAILD -2 //表示入参地址为NULL
|
| 107 |
|
| 108 | #define OUT_MAX_SIZE 1024
|
| 109 | #define LINE __LINE__
|
| 110 | #define FUNC __FUNCTION__
|
| 111 | #define AT_EXTERSION_SOCKET_NAME "/tmp/atcmdext"
|
| 112 | #define SOCKET_ZERO 0
|
| 113 | #define SOCKET_SUCC 1
|
| 114 | #define SOCKET_FAIL -1
|
| 115 |
|
| 116 | typedef void (*mbtk_log)(int level, const char *format,...);
|
| 117 | typedef enum
|
| 118 | {
|
| 119 | A_SUCCESS = 0,
|
| 120 | A_ERROR = -1
|
| 121 | }LYNQ_AT_E;
|
| 122 |
|
| 123 | static mbtk_log fun_ptr_log = NULL;
|
| 124 | void *dlHandle_at = NULL;
|
| 125 | char *lynqLib_at = "/lib/libmbtk_lib.so";
|
| 126 | char *output = NULL;
|
| 127 | int sockfd = 0;
|
| 128 | int result = A_SUCCESS;
|
| 129 | char buffer_at[OUT_MAX_SIZE] = {0};
|
| 130 | struct sockaddr_in addr_serv;
|
| 131 | struct sockaddr_un addr_server;
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 132 | LYNQ_AT_CALLBACK tmp = NULL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 133 | static pthread_mutex_t s_startupMutex = PTHREAD_MUTEX_INITIALIZER;
|
| 134 | socklen_t len;
|
| 135 | bool connect_state = false;
|
| 136 |
|
| 137 |
|
| 138 | int socket_local_client (char* name) {
|
| 139 | LOGD("[%d][%s] enter",LINE,FUNC);
|
| 140 | sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
| 141 | if (sockfd < 0)
|
| 142 | {
|
| 143 | LOGD("Can't open stream socket (%s)", name);
|
| 144 | return -1;
|
| 145 | }
|
| 146 | addr_server.sun_family = AF_UNIX;
|
| 147 | memset(addr_server.sun_path, '\0', sizeof(addr_server.sun_path));
|
| 148 | strncpy(addr_server.sun_path, name, sizeof(addr_server.sun_path) - 1);
|
| 149 | if (connect(sockfd, (struct sockaddr *) &addr_server, sizeof(struct sockaddr_un)) < 0)
|
| 150 | {
|
| 151 | close(sockfd);
|
| 152 | LOGD("Can't connect to server side, path: %s, %s", name, strerror(errno));
|
| 153 | return -1;
|
| 154 | }
|
| 155 | LOGD("[%d][%s] connect %s success",LINE,FUNC,name);
|
| 156 | return sockfd;
|
| 157 | }
|
| 158 | bool send_msg_to_service(int fd,char *msg,int size)
|
| 159 | {
|
| 160 | LOGD("[%d][%s] enter",LINE,FUNC);
|
| 161 | if (fd < 0)
|
| 162 | {
|
| 163 | LOGD("fd invalid when send to atci service. errno = %d", errno);
|
| 164 | return false;
|
| 165 | }
|
| 166 | if(NULL == msg)
|
| 167 | {
|
| 168 | LOGD("atcmd is null.");
|
| 169 | return false;
|
| 170 | }
|
| 171 | int sendLen = send(fd, msg, size, 0);
|
| 172 | if (sendLen != size)
|
| 173 | {
|
| 174 | LOGD("lose data when send to atci service. errno = %d", errno);
|
| 175 | return false;
|
| 176 | }
|
| 177 | LOGD("client send to app demo: %s", msg);
|
| 178 | return true;
|
| 179 | }
|
| 180 |
|
| 181 | int atsvc_cmd_recv(int fd, char *buf, int len)
|
| 182 | {
|
| 183 | int ret = 0;
|
| 184 | ret = recv(fd, buf, len, 0);
|
| 185 | LOGD("[%d][%s] recv after",LINE,FUNC);
|
| 186 | if (ret < 0)
|
| 187 | {
|
| 188 | LOGD("acti_cmd_recv client select error, ret=%d, error=%s(%d),fd=%d", ret,strerror(errno), errno, fd);
|
| 189 | return SOCKET_FAIL;
|
| 190 | }
|
| 191 | else if(ret == 0)
|
| 192 | {
|
| 193 | LOGD("acti_cmd_recv client recv error, ret=%d, error=%s(%d),fd=%d", ret,strerror(errno), errno, fd);
|
| 194 | return SOCKET_ZERO;
|
| 195 | }
|
| 196 | return SOCKET_SUCC;
|
| 197 | }
|
| 198 | /**
|
| 199 | * @brief send third cmd to service and receive input,then send output to service
|
| 200 | *
|
| 201 | * @param parm
|
| 202 | * @return void*
|
| 203 | */
|
| 204 | void *thread_recv(void *parm)
|
| 205 | {
|
| 206 | LOGD("[%d][%s] enter",LINE,FUNC);
|
| 207 | char at_cmd[100] = {0};
|
| 208 | int fd = -1;
|
| 209 | int ret = 0;
|
| 210 | fd = socket_local_client(AT_EXTERSION_SOCKET_NAME);
|
| 211 | if(fd <= 0)
|
| 212 | {
|
| 213 | LOGE("socket_local_client fail\n");
|
| 214 | connect_state = false;
|
| 215 | pthread_mutex_unlock(&s_startupMutex);
|
| 216 | return NULL;
|
| 217 | }
|
| 218 | int len_buf = strlen(buffer_at);
|
| 219 | if(!send_msg_to_service(fd,buffer_at,len_buf))
|
| 220 | {
|
| 221 | LOGE("send_msg_to_service fail\n");
|
| 222 | connect_state = false;
|
| 223 | pthread_mutex_unlock(&s_startupMutex);
|
| 224 | return NULL;
|
| 225 | }
|
| 226 | connect_state = true;
|
| 227 | pthread_mutex_unlock(&s_startupMutex);
|
| 228 | char *input = NULL;
|
| 229 | output = (char *)malloc(sizeof(char)*OUT_MAX_SIZE);
|
| 230 | if(NULL == output)
|
| 231 | {
|
| 232 | LOGE("thread_recv malloc fail\n");
|
| 233 | return NULL;
|
| 234 | }
|
| 235 | TryNewLink:
|
| 236 | if(connect_state == false)
|
| 237 | {
|
| 238 | if (connect(fd, (struct sockaddr *) &addr_server, sizeof(struct sockaddr_un)) < 0)
|
| 239 | {
|
| 240 | close(fd);
|
| 241 | LOGE("Can't connect to server side, path: %s, errno:%d", AT_EXTERSION_SOCKET_NAME, errno);
|
| 242 | return NULL;
|
| 243 | }
|
| 244 | connect_state = true;
|
| 245 | }
|
| 246 | while (1)
|
| 247 | {
|
| 248 | /*receive at cmd*/
|
| 249 | memset(at_cmd, 0, sizeof(at_cmd));
|
| 250 | ret = atsvc_cmd_recv(fd, at_cmd,sizeof(at_cmd));
|
| 251 | if (ret < 0)
|
| 252 | {
|
| 253 | LOGE("[%d][%s]receive CMD error",LINE,FUNC);
|
| 254 | continue;
|
| 255 | }
|
| 256 | else if(ret == SOCKET_ZERO)
|
| 257 | {
|
| 258 | LOGE("maybe client socket closed 1. retry new link!");
|
| 259 | connect_state = false;
|
| 260 | goto TryNewLink;
|
| 261 | }
|
| 262 | input = at_cmd;
|
| 263 | int len = strlen(input);
|
| 264 | while (len > 0 && (input[len - 1] == '\r' || input[len - 1] == '\n'))
|
| 265 | {
|
| 266 | input[--len] = '\0';
|
| 267 | }
|
| 268 | //begin deal with callback
|
| 269 | tmp(input, output, OUT_MAX_SIZE);
|
| 270 | LOGD("lynq_reg_third_at send output to service\n");
|
| 271 | if(!send_msg_to_service(fd,output, strlen(output)))
|
| 272 | {
|
| 273 | LOGE("thread_recv send fail\n");
|
| 274 | continue;
|
| 275 | }
|
| 276 | }
|
| 277 | free(output);
|
| 278 | output = NULL;
|
| 279 | if(fd != 0)
|
| 280 | {
|
| 281 | close(fd);
|
| 282 | }
|
| 283 | return NULL;
|
| 284 | }
|
| 285 |
|
| 286 | /**
|
| 287 | * @brief Threads are created to communicate with the server
|
| 288 | *
|
| 289 | * @return int
|
| 290 | */
|
| 291 | int lynq_connect_service_start(void)
|
| 292 | {
|
| 293 | LOGD("[%d][%s] enter",LINE,FUNC);
|
| 294 | pthread_t lynq_at_tid;
|
| 295 | int rt = pthread_create(&lynq_at_tid, NULL, thread_recv, NULL);
|
| 296 | pthread_mutex_lock(&s_startupMutex);
|
| 297 | LOGD("[%d][%s] pthread mutex unlock",LINE,FUNC);
|
| 298 | if((connect_state != true) && rt < 0)
|
| 299 | {
|
| 300 | LOGE("connect fail,rt:%d,connect state:%d\n",rt,connect_state);
|
| 301 | return -1;
|
| 302 | }
|
| 303 | return 0;
|
| 304 | }
|
| 305 |
|
| 306 | /**
|
| 307 | * @brief Type:[IN] send third at cmd to service
|
| 308 | * @param ext_at Type:[IN] input at cmd
|
| 309 | * @param callback Type:[IN]
|
| 310 | * @return int
|
| 311 | */
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 312 | int32_t gsw_reg_atcmd(const char *atcmd,LYNQ_AT_CALLBACK func)
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 313 | {
|
| 314 | if(NULL == atcmd || NULL == func)
|
| 315 | {
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 316 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 317 | }
|
| 318 | dlHandle_at = dlopen(lynqLib_at, RTLD_NOW);
|
| 319 | fun_ptr_log = (mbtk_log)dlsym(dlHandle_at, "mbtk_log");
|
| 320 | if(fun_ptr_log == NULL || dlHandle_at == NULL)
|
| 321 | {
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 322 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 323 | }
|
| 324 | memcpy(buffer_at, atcmd, strlen(atcmd));
|
| 325 | tmp = func;
|
| 326 | LOGD("lynq_reg_third_at start\n");
|
| 327 | int ret = lynq_connect_service_start();
|
| 328 |
|
| 329 | if(ret != 0)
|
| 330 | {
|
| 331 | LOGE("lynq_connect_service_start start failed\n");
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 332 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 333 | }
|
| 334 | LOGD("lynq_connect_service_start success ret:%d\n",ret);
|
| 335 | return GSW_HAL_SUCCESS;
|
| 336 | }
|
| 337 |
|
| 338 | int32_t gsw_sdk_at_init(void)
|
| 339 | {
|
| 340 | return GSW_HAL_SUCCESS;
|
| 341 | }
|
lichengzhang | b93f786 | 2025-06-06 17:44:35 +0800 | [diff] [blame^] | 342 |
|
| 343 | int gsw_get_modem_temperture(ZONE_NUM num,int *temp)
|
| 344 | {
|
| 345 | if (num == soc_max)
|
| 346 | {
|
| 347 | char *cmd = "/sys/class/thermal/thermal_zone0/temp";
|
| 348 | FILE *fp;
|
| 349 | char buf[128];
|
| 350 | fp = fopen(cmd, "r");
|
| 351 | if (fp == NULL)
|
| 352 | {
|
| 353 | perror("Unable to open file");
|
| 354 | return GSW_HAL_NORMAL_FAIL;
|
| 355 | }
|
| 356 |
|
| 357 | if (fgets(buf, sizeof(buf), fp) != NULL)
|
| 358 | {
|
| 359 | *temp = atoi(buf) * 0.001;
|
| 360 | //printf("Temperature: %d °C\n", *temp);
|
| 361 | }
|
| 362 |
|
| 363 | fclose(fp);
|
| 364 | return GSW_HAL_SUCCESS;
|
| 365 | }
|
| 366 | else
|
| 367 | return GSW_HAL_FUNC_UNSUPPORT;
|
| 368 | }
|