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"
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 17 | #include "gsw_hal_errcode.h"
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 18 | #include "gsw_log_interface.h"
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 19 |
|
| 20 | #define OUT_MAX_SIZE 1024
|
| 21 | #define LINE __LINE__
|
| 22 | #define FUNC __FUNCTION__
|
| 23 | #define AT_EXTERSION_SOCKET_NAME "/tmp/atcmdext"
|
| 24 | #define SOCKET_ZERO 0
|
| 25 | #define SOCKET_SUCC 1
|
| 26 | #define SOCKET_FAIL -1
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 27 | #define GSW_AT "[HAL][GSW_AT]"
|
| 28 | //typedef void (*mbtk_log)(int level, const char *format,...);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 29 | typedef enum
|
| 30 | {
|
| 31 | A_SUCCESS = 0,
|
| 32 | A_ERROR = -1
|
| 33 | }LYNQ_AT_E;
|
| 34 |
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 35 | //static mbtk_log fun_ptr_log = NULL;
|
lichengzhang | ea38e90 | 2025-06-14 11:53:03 +0800 | [diff] [blame] | 36 | static void *dlHandle_at = NULL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 37 | char *lynqLib_at = "/lib/libmbtk_lib.so";
|
| 38 | char *output = NULL;
|
| 39 | int sockfd = 0;
|
| 40 | int result = A_SUCCESS;
|
| 41 | char buffer_at[OUT_MAX_SIZE] = {0};
|
| 42 | struct sockaddr_in addr_serv;
|
| 43 | struct sockaddr_un addr_server;
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 44 | LYNQ_AT_CALLBACK tmp = NULL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 45 | static pthread_mutex_t s_startupMutex = PTHREAD_MUTEX_INITIALIZER;
|
| 46 | socklen_t len;
|
| 47 | bool connect_state = false;
|
| 48 |
|
| 49 |
|
| 50 | int socket_local_client (char* name) {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 51 | LOGD(GSW_AT,"[%d][%s] enter",LINE,FUNC);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 52 | sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
| 53 | if (sockfd < 0)
|
| 54 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 55 | LOGD(GSW_AT,"Can't open stream socket (%s)", name);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 56 | return -1;
|
| 57 | }
|
| 58 | addr_server.sun_family = AF_UNIX;
|
| 59 | memset(addr_server.sun_path, '\0', sizeof(addr_server.sun_path));
|
| 60 | strncpy(addr_server.sun_path, name, sizeof(addr_server.sun_path) - 1);
|
| 61 | if (connect(sockfd, (struct sockaddr *) &addr_server, sizeof(struct sockaddr_un)) < 0)
|
| 62 | {
|
| 63 | close(sockfd);
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 64 | LOGD(GSW_AT,"Can't connect to server side, path: %s, %s", name, strerror(errno));
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 65 | return -1;
|
| 66 | }
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 67 | LOGD(GSW_AT,"[%d][%s] connect %s success",LINE,FUNC,name);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 68 | return sockfd;
|
| 69 | }
|
| 70 | bool send_msg_to_service(int fd,char *msg,int size)
|
| 71 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 72 | LOGD(GSW_AT,"[%d][%s] enter",LINE,FUNC);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 73 | if (fd < 0)
|
| 74 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 75 | LOGD(GSW_AT,"fd invalid when send to atci service. errno = %d", errno);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 76 | return false;
|
| 77 | }
|
| 78 | if(NULL == msg)
|
| 79 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 80 | LOGD(GSW_AT,"atcmd is null.");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 81 | return false;
|
| 82 | }
|
| 83 | int sendLen = send(fd, msg, size, 0);
|
| 84 | if (sendLen != size)
|
| 85 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 86 | LOGD(GSW_AT,"lose data when send to atci service. errno = %d", errno);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 87 | return false;
|
| 88 | }
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 89 | LOGD(GSW_AT,"client send to app demo: %s", msg);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 90 | return true;
|
| 91 | }
|
| 92 |
|
| 93 | int atsvc_cmd_recv(int fd, char *buf, int len)
|
| 94 | {
|
| 95 | int ret = 0;
|
| 96 | ret = recv(fd, buf, len, 0);
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 97 | LOGD(GSW_AT,"[%d][%s] recv after",LINE,FUNC);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 98 | if (ret < 0)
|
| 99 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 100 | LOGD(GSW_AT,"acti_cmd_recv client select error, ret=%d, error=%s(%d),fd=%d", ret,strerror(errno), errno, fd);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 101 | return SOCKET_FAIL;
|
| 102 | }
|
| 103 | else if(ret == 0)
|
| 104 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 105 | LOGD(GSW_AT,"acti_cmd_recv client recv error, ret=%d, error=%s(%d),fd=%d", ret,strerror(errno), errno, fd);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 106 | return SOCKET_ZERO;
|
| 107 | }
|
| 108 | return SOCKET_SUCC;
|
| 109 | }
|
| 110 | /**
|
| 111 | * @brief send third cmd to service and receive input,then send output to service
|
| 112 | *
|
| 113 | * @param parm
|
| 114 | * @return void*
|
| 115 | */
|
| 116 | void *thread_recv(void *parm)
|
| 117 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 118 | LOGD(GSW_AT,"[%d][%s] enter",LINE,FUNC);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 119 | char at_cmd[100] = {0};
|
| 120 | int fd = -1;
|
| 121 | int ret = 0;
|
| 122 | fd = socket_local_client(AT_EXTERSION_SOCKET_NAME);
|
| 123 | if(fd <= 0)
|
| 124 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 125 | LOGE(GSW_AT,"socket_local_client fail\n");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 126 | connect_state = false;
|
| 127 | pthread_mutex_unlock(&s_startupMutex);
|
| 128 | return NULL;
|
| 129 | }
|
| 130 | int len_buf = strlen(buffer_at);
|
| 131 | if(!send_msg_to_service(fd,buffer_at,len_buf))
|
| 132 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 133 | LOGE(GSW_AT,"send_msg_to_service fail\n");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 134 | connect_state = false;
|
| 135 | pthread_mutex_unlock(&s_startupMutex);
|
| 136 | return NULL;
|
| 137 | }
|
| 138 | connect_state = true;
|
| 139 | pthread_mutex_unlock(&s_startupMutex);
|
| 140 | char *input = NULL;
|
| 141 | output = (char *)malloc(sizeof(char)*OUT_MAX_SIZE);
|
| 142 | if(NULL == output)
|
| 143 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 144 | LOGE(GSW_AT,"thread_recv malloc fail\n");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 145 | return NULL;
|
| 146 | }
|
| 147 | TryNewLink:
|
| 148 | if(connect_state == false)
|
| 149 | {
|
| 150 | if (connect(fd, (struct sockaddr *) &addr_server, sizeof(struct sockaddr_un)) < 0)
|
| 151 | {
|
| 152 | close(fd);
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 153 | LOGE(GSW_AT,"Can't connect to server side, path: %s, errno:%d", AT_EXTERSION_SOCKET_NAME, errno);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 154 | return NULL;
|
| 155 | }
|
| 156 | connect_state = true;
|
| 157 | }
|
| 158 | while (1)
|
| 159 | {
|
| 160 | /*receive at cmd*/
|
| 161 | memset(at_cmd, 0, sizeof(at_cmd));
|
| 162 | ret = atsvc_cmd_recv(fd, at_cmd,sizeof(at_cmd));
|
| 163 | if (ret < 0)
|
| 164 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 165 | LOGE(GSW_AT,"[%d][%s]receive CMD error",LINE,FUNC);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 166 | continue;
|
| 167 | }
|
| 168 | else if(ret == SOCKET_ZERO)
|
| 169 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 170 | LOGE(GSW_AT,"maybe client socket closed 1. retry new link!");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 171 | connect_state = false;
|
| 172 | goto TryNewLink;
|
| 173 | }
|
| 174 | input = at_cmd;
|
| 175 | int len = strlen(input);
|
| 176 | while (len > 0 && (input[len - 1] == '\r' || input[len - 1] == '\n'))
|
| 177 | {
|
| 178 | input[--len] = '\0';
|
| 179 | }
|
| 180 | //begin deal with callback
|
| 181 | tmp(input, output, OUT_MAX_SIZE);
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 182 | LOGD(GSW_AT,"lynq_reg_third_at send output to service\n");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 183 | if(!send_msg_to_service(fd,output, strlen(output)))
|
| 184 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 185 | LOGE(GSW_AT,"thread_recv send fail\n");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 186 | continue;
|
| 187 | }
|
| 188 | }
|
| 189 | free(output);
|
| 190 | output = NULL;
|
| 191 | if(fd != 0)
|
| 192 | {
|
| 193 | close(fd);
|
| 194 | }
|
| 195 | return NULL;
|
| 196 | }
|
| 197 |
|
| 198 | /**
|
| 199 | * @brief Threads are created to communicate with the server
|
| 200 | *
|
| 201 | * @return int
|
| 202 | */
|
| 203 | int lynq_connect_service_start(void)
|
| 204 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 205 | LOGD(GSW_AT,"[%d][%s] enter",LINE,FUNC);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 206 | pthread_t lynq_at_tid;
|
| 207 | int rt = pthread_create(&lynq_at_tid, NULL, thread_recv, NULL);
|
| 208 | pthread_mutex_lock(&s_startupMutex);
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 209 | LOGD(GSW_AT,"[%d][%s] pthread mutex unlock",LINE,FUNC);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 210 | if((connect_state != true) && rt < 0)
|
| 211 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 212 | LOGE(GSW_AT,"connect fail,rt:%d,connect state:%d\n",rt,connect_state);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 213 | return -1;
|
| 214 | }
|
| 215 | return 0;
|
| 216 | }
|
| 217 |
|
| 218 | /**
|
| 219 | * @brief Type:[IN] send third at cmd to service
|
| 220 | * @param ext_at Type:[IN] input at cmd
|
| 221 | * @param callback Type:[IN]
|
| 222 | * @return int
|
| 223 | */
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 224 | int32_t gsw_reg_atcmd(const char *atcmd,LYNQ_AT_CALLBACK func)
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 225 | {
|
| 226 | if(NULL == atcmd || NULL == func)
|
| 227 | {
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 228 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 229 | }
|
lichengzhang | ea38e90 | 2025-06-14 11:53:03 +0800 | [diff] [blame] | 230 |
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 231 | memcpy(buffer_at, atcmd, strlen(atcmd));
|
| 232 | tmp = func;
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 233 | LOGD(GSW_AT,"lynq_reg_third_at start\n");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 234 | int ret = lynq_connect_service_start();
|
| 235 |
|
| 236 | if(ret != 0)
|
| 237 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 238 | LOGE(GSW_AT,"lynq_connect_service_start start failed\n");
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 239 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 240 | }
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 241 | LOGD(GSW_AT,"lynq_connect_service_start success ret:%d\n",ret);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 242 | return GSW_HAL_SUCCESS;
|
| 243 | }
|
| 244 |
|
| 245 | int32_t gsw_sdk_at_init(void)
|
| 246 | {
|
lichengzhang | ea38e90 | 2025-06-14 11:53:03 +0800 | [diff] [blame] | 247 | dlHandle_at = dlopen(lynqLib_at, RTLD_NOW);
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 248 | //fun_ptr_log = (mbtk_log)dlsym(dlHandle_at, "mbtk_log");
|
| 249 | if(dlHandle_at == NULL)
|
lichengzhang | ea38e90 | 2025-06-14 11:53:03 +0800 | [diff] [blame] | 250 | {
|
| 251 | return GSW_HAL_NORMAL_FAIL;
|
| 252 | }
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 253 | return GSW_HAL_SUCCESS;
|
| 254 | }
|
lichengzhang | b93f786 | 2025-06-06 17:44:35 +0800 | [diff] [blame] | 255 |
|
| 256 | int gsw_get_modem_temperture(ZONE_NUM num,int *temp)
|
| 257 | {
|
lichengzhang | 4d12ebc | 2025-06-11 17:49:57 +0800 | [diff] [blame] | 258 | if (num != soc_max)
|
lichengzhang | b93f786 | 2025-06-06 17:44:35 +0800 | [diff] [blame] | 259 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 260 | LOGE(GSW_AT,"temperture if not support,num is %d\n",num);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 261 | return GSW_HAL_SUCCESS;
|
lichengzhang | b93f786 | 2025-06-06 17:44:35 +0800 | [diff] [blame] | 262 | }
|
lichengzhang | 4d12ebc | 2025-06-11 17:49:57 +0800 | [diff] [blame] | 263 | if (NULL == temp)
|
| 264 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 265 | LOGE(GSW_AT,"temperture is null\n");
|
lichengzhang | 4d12ebc | 2025-06-11 17:49:57 +0800 | [diff] [blame] | 266 | return GSW_HAL_ARG_INVALID;
|
| 267 | }
|
| 268 |
|
| 269 | char *cmd = "/sys/class/thermal/thermal_zone0/temp";
|
| 270 | FILE *fp = NULL;
|
| 271 | char buf[128] = {0};
|
| 272 | fp = fopen(cmd, "r");
|
| 273 | if (fp == NULL)
|
| 274 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 275 | LOGE(GSW_AT,"Unable to open file");
|
lichengzhang | 4d12ebc | 2025-06-11 17:49:57 +0800 | [diff] [blame] | 276 | return GSW_HAL_NORMAL_FAIL;
|
| 277 | }
|
| 278 | if (fgets(buf, sizeof(buf), fp) == NULL)
|
| 279 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 280 | LOGE(GSW_AT,"fgets fail");
|
lichengzhang | 4d12ebc | 2025-06-11 17:49:57 +0800 | [diff] [blame] | 281 | fclose(fp);
|
| 282 | return GSW_HAL_NORMAL_FAIL;
|
| 283 | }
|
| 284 | if (strlen(buf) == 0)
|
| 285 | {
|
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame^] | 286 | LOGE(GSW_AT,"read len == 0\n");
|
lichengzhang | 4d12ebc | 2025-06-11 17:49:57 +0800 | [diff] [blame] | 287 | fclose(fp);
|
| 288 | return GSW_HAL_NORMAL_FAIL;
|
| 289 | }
|
| 290 |
|
| 291 | *temp = atoi(buf) / 1000;
|
| 292 | fclose(fp);
|
| 293 | return GSW_HAL_SUCCESS;
|
lichengzhang | b93f786 | 2025-06-06 17:44:35 +0800 | [diff] [blame] | 294 | }
|