#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <sys/un.h> | |
#include <unistd.h> | |
#include <arpa/inet.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <signal.h> | |
#include <string.h> | |
#include <pthread.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <stdint.h> | |
#include <dlfcn.h> | |
#include <stdbool.h> | |
#include "gsw_at_interface.h" | |
#include "gsw_hal_errcode.h" | |
#include "gsw_log_interface.h" | |
#define OUT_MAX_SIZE 1024 | |
#define LINE __LINE__ | |
#define FUNC __FUNCTION__ | |
#define AT_EXTERSION_SOCKET_NAME "/tmp/atcmdext" | |
#define SOCKET_ZERO 0 | |
#define SOCKET_SUCC 1 | |
#define SOCKET_FAIL -1 | |
#define GSW_AT "[HAL][GSW_AT]" | |
//typedef void (*mbtk_log)(int level, const char *format,...); | |
typedef enum | |
{ | |
A_SUCCESS = 0, | |
A_ERROR = -1 | |
}LYNQ_AT_E; | |
//static mbtk_log fun_ptr_log = NULL; | |
static void *dlHandle_at = NULL; | |
char *lynqLib_at = "/lib/libmbtk_lib.so"; | |
char *output = NULL; | |
int sockfd = 0; | |
int result = A_SUCCESS; | |
char buffer_at[OUT_MAX_SIZE] = {0}; | |
struct sockaddr_in addr_serv; | |
struct sockaddr_un addr_server; | |
LYNQ_AT_CALLBACK tmp = NULL; | |
static pthread_mutex_t s_startupMutex = PTHREAD_MUTEX_INITIALIZER; | |
socklen_t len; | |
bool connect_state = false; | |
int socket_local_client (char* name) { | |
LOGD(GSW_AT,"[%d][%s] enter",LINE,FUNC); | |
sockfd = socket(AF_UNIX, SOCK_STREAM, 0); | |
if (sockfd < 0) | |
{ | |
LOGD(GSW_AT,"Can't open stream socket (%s)", name); | |
return -1; | |
} | |
addr_server.sun_family = AF_UNIX; | |
memset(addr_server.sun_path, '\0', sizeof(addr_server.sun_path)); | |
strncpy(addr_server.sun_path, name, sizeof(addr_server.sun_path) - 1); | |
if (connect(sockfd, (struct sockaddr *) &addr_server, sizeof(struct sockaddr_un)) < 0) | |
{ | |
close(sockfd); | |
LOGD(GSW_AT,"Can't connect to server side, path: %s, %s", name, strerror(errno)); | |
return -1; | |
} | |
LOGD(GSW_AT,"[%d][%s] connect %s success",LINE,FUNC,name); | |
return sockfd; | |
} | |
bool send_msg_to_service(int fd,char *msg,int size) | |
{ | |
LOGD(GSW_AT,"[%d][%s] enter",LINE,FUNC); | |
if (fd < 0) | |
{ | |
LOGD(GSW_AT,"fd invalid when send to atci service. errno = %d", errno); | |
return false; | |
} | |
if(NULL == msg) | |
{ | |
LOGD(GSW_AT,"atcmd is null."); | |
return false; | |
} | |
int sendLen = send(fd, msg, size, 0); | |
if (sendLen != size) | |
{ | |
LOGD(GSW_AT,"lose data when send to atci service. errno = %d", errno); | |
return false; | |
} | |
LOGD(GSW_AT,"client send to app demo: %s", msg); | |
return true; | |
} | |
int atsvc_cmd_recv(int fd, char *buf, int len) | |
{ | |
int ret = 0; | |
ret = recv(fd, buf, len, 0); | |
LOGD(GSW_AT,"[%d][%s] recv after",LINE,FUNC); | |
if (ret < 0) | |
{ | |
LOGD(GSW_AT,"acti_cmd_recv client select error, ret=%d, error=%s(%d),fd=%d", ret,strerror(errno), errno, fd); | |
return SOCKET_FAIL; | |
} | |
else if(ret == 0) | |
{ | |
LOGD(GSW_AT,"acti_cmd_recv client recv error, ret=%d, error=%s(%d),fd=%d", ret,strerror(errno), errno, fd); | |
return SOCKET_ZERO; | |
} | |
return SOCKET_SUCC; | |
} | |
/** | |
* @brief send third cmd to service and receive input,then send output to service | |
* | |
* @param parm | |
* @return void* | |
*/ | |
void *thread_recv(void *parm) | |
{ | |
LOGD(GSW_AT,"[%d][%s] enter",LINE,FUNC); | |
char at_cmd[100] = {0}; | |
int fd = -1; | |
int ret = 0; | |
fd = socket_local_client(AT_EXTERSION_SOCKET_NAME); | |
if(fd <= 0) | |
{ | |
LOGE(GSW_AT,"socket_local_client fail\n"); | |
connect_state = false; | |
pthread_mutex_unlock(&s_startupMutex); | |
return NULL; | |
} | |
int len_buf = strlen(buffer_at); | |
if(!send_msg_to_service(fd,buffer_at,len_buf)) | |
{ | |
LOGE(GSW_AT,"send_msg_to_service fail\n"); | |
connect_state = false; | |
pthread_mutex_unlock(&s_startupMutex); | |
return NULL; | |
} | |
connect_state = true; | |
pthread_mutex_unlock(&s_startupMutex); | |
char *input = NULL; | |
output = (char *)malloc(sizeof(char)*OUT_MAX_SIZE); | |
if(NULL == output) | |
{ | |
LOGE(GSW_AT,"thread_recv malloc fail\n"); | |
return NULL; | |
} | |
TryNewLink: | |
if(connect_state == false) | |
{ | |
if (connect(fd, (struct sockaddr *) &addr_server, sizeof(struct sockaddr_un)) < 0) | |
{ | |
close(fd); | |
LOGE(GSW_AT,"Can't connect to server side, path: %s, errno:%d", AT_EXTERSION_SOCKET_NAME, errno); | |
return NULL; | |
} | |
connect_state = true; | |
} | |
while (1) | |
{ | |
/*receive at cmd*/ | |
memset(at_cmd, 0, sizeof(at_cmd)); | |
ret = atsvc_cmd_recv(fd, at_cmd,sizeof(at_cmd)); | |
if (ret < 0) | |
{ | |
LOGE(GSW_AT,"[%d][%s]receive CMD error",LINE,FUNC); | |
continue; | |
} | |
else if(ret == SOCKET_ZERO) | |
{ | |
LOGE(GSW_AT,"maybe client socket closed 1. retry new link!"); | |
connect_state = false; | |
goto TryNewLink; | |
} | |
input = at_cmd; | |
int len = strlen(input); | |
while (len > 0 && (input[len - 1] == '\r' || input[len - 1] == '\n')) | |
{ | |
input[--len] = '\0'; | |
} | |
//begin deal with callback | |
tmp(input, output, OUT_MAX_SIZE); | |
LOGD(GSW_AT,"lynq_reg_third_at send output to service\n"); | |
if(!send_msg_to_service(fd,output, strlen(output))) | |
{ | |
LOGE(GSW_AT,"thread_recv send fail\n"); | |
continue; | |
} | |
} | |
free(output); | |
output = NULL; | |
if(fd != 0) | |
{ | |
close(fd); | |
} | |
return NULL; | |
} | |
/** | |
* @brief Threads are created to communicate with the server | |
* | |
* @return int | |
*/ | |
int lynq_connect_service_start(void) | |
{ | |
LOGD(GSW_AT,"[%d][%s] enter",LINE,FUNC); | |
pthread_t lynq_at_tid; | |
int rt = pthread_create(&lynq_at_tid, NULL, thread_recv, NULL); | |
pthread_mutex_lock(&s_startupMutex); | |
LOGD(GSW_AT,"[%d][%s] pthread mutex unlock",LINE,FUNC); | |
if((connect_state != true) && rt < 0) | |
{ | |
LOGE(GSW_AT,"connect fail,rt:%d,connect state:%d\n",rt,connect_state); | |
return -1; | |
} | |
return 0; | |
} | |
/** | |
* @brief Type:[IN] send third at cmd to service | |
* @param ext_at Type:[IN] input at cmd | |
* @param callback Type:[IN] | |
* @return int | |
*/ | |
int32_t gsw_reg_atcmd(const char *atcmd,LYNQ_AT_CALLBACK func) | |
{ | |
if(NULL == atcmd || NULL == func) | |
{ | |
return GSW_HAL_NORMAL_FAIL; | |
} | |
memcpy(buffer_at, atcmd, strlen(atcmd)); | |
tmp = func; | |
LOGD(GSW_AT,"lynq_reg_third_at start\n"); | |
int ret = lynq_connect_service_start(); | |
if(ret != 0) | |
{ | |
LOGE(GSW_AT,"lynq_connect_service_start start failed\n"); | |
return GSW_HAL_NORMAL_FAIL; | |
} | |
LOGD(GSW_AT,"lynq_connect_service_start success ret:%d\n",ret); | |
return GSW_HAL_SUCCESS; | |
} | |
int32_t gsw_sdk_at_init(void) | |
{ | |
dlHandle_at = dlopen(lynqLib_at, RTLD_NOW); | |
//fun_ptr_log = (mbtk_log)dlsym(dlHandle_at, "mbtk_log"); | |
if(dlHandle_at == NULL) | |
{ | |
return GSW_HAL_NORMAL_FAIL; | |
} | |
return GSW_HAL_SUCCESS; | |
} | |
int gsw_get_modem_temperture(ZONE_NUM num,int *temp) | |
{ | |
if (num != soc_max) | |
{ | |
LOGE(GSW_AT,"temperture if not support,num is %d\n",num); | |
return GSW_HAL_SUCCESS; | |
} | |
if (NULL == temp) | |
{ | |
LOGE(GSW_AT,"temperture is null\n"); | |
return GSW_HAL_ARG_INVALID; | |
} | |
char *cmd = "/sys/class/thermal/thermal_zone0/temp"; | |
FILE *fp = NULL; | |
char buf[128] = {0}; | |
fp = fopen(cmd, "r"); | |
if (fp == NULL) | |
{ | |
LOGE(GSW_AT,"Unable to open file"); | |
return GSW_HAL_NORMAL_FAIL; | |
} | |
if (fgets(buf, sizeof(buf), fp) == NULL) | |
{ | |
LOGE(GSW_AT,"fgets fail"); | |
fclose(fp); | |
return GSW_HAL_NORMAL_FAIL; | |
} | |
if (strlen(buf) == 0) | |
{ | |
LOGE(GSW_AT,"read len == 0\n"); | |
fclose(fp); | |
return GSW_HAL_NORMAL_FAIL; | |
} | |
*temp = atoi(buf) / 1000; | |
fclose(fp); | |
return GSW_HAL_SUCCESS; | |
} |