xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /**
|
| 2 | * @file at_thread.c
|
| 3 | * @brief Implementation of at_thread.c.
|
| 4 | *
|
| 5 | * Copyright (C) 2022 Sanechips Technology Co., Ltd.
|
| 6 | * @author
|
| 7 | *
|
| 8 | * This program is free software; you can redistribute it and/or modify
|
| 9 | * it under the terms of the GNU General Public License version 2 as
|
| 10 | * published by the Free Software Foundation. £¨±ØÑ¡£ºGPLv2 Licence£©
|
| 11 | *
|
| 12 | */
|
| 13 |
|
| 14 |
|
| 15 | /*******************************************************************************
|
| 16 | * Include header files *
|
| 17 | *******************************************************************************/
|
| 18 | #include <stdio.h>
|
| 19 | #include <string.h>
|
| 20 | #include <stdlib.h>
|
| 21 | #include <pthread.h>
|
| 22 | #include <sys/prctl.h>
|
| 23 | #include <sys/ipc.h>
|
| 24 | #include <errno.h>
|
| 25 |
|
| 26 | #include "atreg_common.h"
|
| 27 | #include "message.h"
|
| 28 | #include "softap_log.h"
|
| 29 | #include "atreg_msg.h"
|
| 30 |
|
| 31 |
|
| 32 | /*******************************************************************************
|
| 33 | * Macro definitions *
|
| 34 | *******************************************************************************/
|
| 35 |
|
| 36 |
|
| 37 | /*******************************************************************************
|
| 38 | * Type definitions *
|
| 39 | *******************************************************************************/
|
| 40 |
|
| 41 |
|
| 42 | /*******************************************************************************
|
| 43 | * Local variable definitions *
|
| 44 | *******************************************************************************/
|
| 45 |
|
| 46 |
|
| 47 | /*******************************************************************************
|
| 48 | * Global variable definitions *
|
| 49 | *******************************************************************************/
|
| 50 |
|
| 51 |
|
| 52 | /*******************************************************************************
|
| 53 | * Local function declarations *
|
| 54 | *******************************************************************************/
|
| 55 |
|
| 56 |
|
| 57 | /*******************************************************************************
|
| 58 | * Local function implementations *
|
| 59 | *******************************************************************************/
|
| 60 | static void atreg_ser_regist_rsp_proc(struct atreg_msg_t *patreg_msgdata)
|
| 61 | {
|
| 62 | struct atreg_ser_instance_t *patreg_ser_instance = NULL;
|
| 63 |
|
| 64 | if (0 == patreg_msgdata->res) {
|
| 65 | patreg_ser_instance = (struct atreg_ser_instance_t *)atreg_search_instance_tmp_by_reqid(patreg_msgdata->req_msg_id, AT_REG_SER);
|
| 66 | if (NULL != patreg_ser_instance) {
|
| 67 | pthread_mutex_lock(&atreg_ser_ctx.at_ser_lock_tmp);
|
| 68 | list_del((struct list_head *)patreg_ser_instance);
|
| 69 | pthread_mutex_unlock(&atreg_ser_ctx.at_ser_lock_tmp);
|
| 70 |
|
| 71 | pthread_mutex_lock(&atreg_ser_ctx.at_ser_lock);
|
| 72 | list_add_tail((struct list_head *)patreg_ser_instance, &atreg_ser_ctx.at_ser_list);
|
| 73 | pthread_mutex_unlock(&atreg_ser_ctx.at_ser_lock);
|
| 74 |
|
| 75 | slog(ATREG_PRINT, SLOG_NORMAL, "AT %s regist success!\n", patreg_msgdata->at_cmd_prefix);
|
| 76 | }
|
| 77 | } else {
|
| 78 | patreg_ser_instance = (struct atreg_ser_instance_t *)atreg_search_instance_tmp_by_reqid(patreg_msgdata->req_msg_id, AT_REG_SER);
|
| 79 | pthread_mutex_lock(&atreg_ser_ctx.at_ser_lock_tmp);
|
| 80 | list_del((struct list_head *)patreg_ser_instance);
|
| 81 | pthread_mutex_unlock(&atreg_ser_ctx.at_ser_lock_tmp);
|
| 82 |
|
| 83 | slog(ATREG_PRINT, SLOG_ERR, "AT %s regist fail!\n", patreg_msgdata->at_cmd_prefix);
|
| 84 |
|
| 85 | free(patreg_ser_instance);
|
| 86 | }
|
| 87 |
|
| 88 | return;
|
| 89 | }
|
| 90 |
|
| 91 |
|
| 92 | static void atreg_info_regist_rsp_proc(struct atreg_msg_t *patreg_msgdata)
|
| 93 | {
|
| 94 | struct atreg_info_instance_t *patreg_info_instance = NULL;
|
| 95 |
|
| 96 | if (0 == patreg_msgdata->res) {
|
| 97 | patreg_info_instance = (struct atreg_info_instance_t *)atreg_search_instance_tmp_by_reqid(patreg_msgdata->req_msg_id, AT_REG_INFO);
|
| 98 | if (NULL != patreg_info_instance) {
|
| 99 | pthread_mutex_lock(&atreg_info_ctx.at_info_lock_tmp);
|
| 100 | list_del((struct list_head *)patreg_info_instance);
|
| 101 | pthread_mutex_unlock(&atreg_info_ctx.at_info_lock_tmp);
|
| 102 |
|
| 103 | pthread_mutex_lock(&atreg_info_ctx.at_info_lock);
|
| 104 | list_add_tail((struct list_head *)patreg_info_instance, &atreg_info_ctx.at_info_list);
|
| 105 | pthread_mutex_unlock(&atreg_info_ctx.at_info_lock);
|
| 106 |
|
| 107 | slog(ATREG_PRINT, SLOG_NORMAL, "AT %s regist success!\n", patreg_msgdata->at_cmd_prefix);
|
| 108 | }
|
| 109 | } else {
|
| 110 | patreg_info_instance = (struct atreg_info_instance_t *)atreg_search_instance_tmp_by_reqid(patreg_msgdata->req_msg_id, AT_REG_INFO);
|
| 111 | pthread_mutex_lock(&atreg_info_ctx.at_info_lock_tmp);
|
| 112 | list_del((struct list_head *)patreg_info_instance);
|
| 113 | pthread_mutex_unlock(&atreg_info_ctx.at_info_lock_tmp);
|
| 114 |
|
| 115 | slog(ATREG_PRINT, SLOG_ERR, "AT %s regist fail!\n", patreg_msgdata->at_cmd_prefix);
|
| 116 |
|
| 117 | free(patreg_info_instance);
|
| 118 | }
|
| 119 |
|
| 120 | return;
|
| 121 | }
|
| 122 |
|
| 123 |
|
| 124 | static void atreg_regist_rsp_proc(unsigned char *aucDataBuf)
|
| 125 | {
|
| 126 | struct atreg_msg_t *patreg_msgdata = (struct atreg_msg_t *)aucDataBuf;
|
| 127 |
|
| 128 | switch (patreg_msgdata->type) {
|
| 129 | case AT_REG_SER:
|
| 130 | atreg_ser_regist_rsp_proc(patreg_msgdata);
|
| 131 | break;
|
| 132 |
|
| 133 | case AT_REG_INFO:
|
| 134 | atreg_info_regist_rsp_proc(patreg_msgdata);
|
| 135 | break;
|
| 136 |
|
| 137 | default:
|
| 138 | break;
|
| 139 | }
|
| 140 |
|
| 141 | sem_post(&atreg_common_ctx.sem_id);
|
| 142 |
|
| 143 | return;
|
| 144 | }
|
| 145 |
|
| 146 |
|
| 147 | static void atreg_ser_unregist_rsp_proc(struct atreg_msg_t *patreg_msgdata)
|
| 148 | {
|
| 149 | int i = -1;
|
| 150 | int j = -1;
|
| 151 | struct atreg_ser_instance_t *patreg_ser_instance = NULL;
|
| 152 |
|
| 153 | if (0 == patreg_msgdata->res) {
|
| 154 | patreg_ser_instance = (struct atreg_ser_instance_t *)atreg_search_instance_by_prefix(patreg_msgdata->at_cmd_prefix, AT_REG_SER);
|
| 155 | if (NULL != patreg_ser_instance) {
|
| 156 | pthread_mutex_lock(&atreg_ser_ctx.at_ser_idpool_lock);
|
| 157 | i = patreg_ser_instance->req_msg_id / 8;
|
| 158 | j = patreg_ser_instance->req_msg_id % 8;
|
| 159 | atreg_ser_dynamic_idpool[i] = atreg_ser_dynamic_idpool[i] & (~(1 << (7 - j)));
|
| 160 | atreg_ser_dynamic_idpool[i] = atreg_ser_dynamic_idpool[i] & (~(1 << (6 - j)));
|
| 161 | pthread_mutex_unlock(&atreg_ser_ctx.at_ser_idpool_lock);
|
| 162 |
|
| 163 | pthread_mutex_lock(&atreg_ser_ctx.at_ser_lock);
|
| 164 | list_del((struct list_head *)patreg_ser_instance);
|
| 165 | pthread_mutex_unlock(&atreg_ser_ctx.at_ser_lock);
|
| 166 |
|
| 167 | free(patreg_ser_instance);
|
| 168 |
|
| 169 | slog(ATREG_PRINT, SLOG_NORMAL, "AT %s unregist success!\n", patreg_msgdata->at_cmd_prefix);
|
| 170 | }
|
| 171 | } else {
|
| 172 | slog(ATREG_PRINT, SLOG_ERR, "AT %s unregist fail!\n", patreg_msgdata->at_cmd_prefix);
|
| 173 | }
|
| 174 |
|
| 175 | return;
|
| 176 | }
|
| 177 |
|
| 178 |
|
| 179 | static void atreg_info_unregist_rsp_proc(struct atreg_msg_t *patreg_msgdata)
|
| 180 | {
|
| 181 | int i = -1;
|
| 182 | int j = -1;
|
| 183 | struct atreg_info_instance_t *patreg_info_instance = NULL;
|
| 184 |
|
| 185 | if (0 == patreg_msgdata->res) {
|
| 186 | patreg_info_instance = (struct atreg_info_instance_t *)atreg_search_instance_by_prefix(patreg_msgdata->at_cmd_prefix, AT_REG_INFO);
|
| 187 | if (NULL != patreg_info_instance) {
|
| 188 | pthread_mutex_lock(&atreg_info_ctx.at_info_idpool_lock);
|
| 189 | i = (patreg_info_instance->req_msg_id - 512) / 8;
|
| 190 | j = (patreg_info_instance->req_msg_id -512) % 8;
|
| 191 | atreg_info_dynamic_idpool[i] = atreg_info_dynamic_idpool[i] & (~(1 << (7 - j)));
|
| 192 | pthread_mutex_unlock(&atreg_info_ctx.at_info_idpool_lock);
|
| 193 |
|
| 194 | pthread_mutex_lock(&atreg_info_ctx.at_info_lock);
|
| 195 | list_del((struct list_head *)patreg_info_instance);
|
| 196 | pthread_mutex_unlock(&atreg_info_ctx.at_info_lock);
|
| 197 |
|
| 198 | free(patreg_info_instance);
|
| 199 |
|
| 200 | slog(ATREG_PRINT, SLOG_NORMAL, "AT %s unregist success!\n", patreg_msgdata->at_cmd_prefix);
|
| 201 | }
|
| 202 | } else {
|
| 203 | slog(ATREG_PRINT, SLOG_ERR, "AT %s unregist fail!\n", patreg_msgdata->at_cmd_prefix);
|
| 204 | }
|
| 205 |
|
| 206 | return;
|
| 207 | }
|
| 208 |
|
| 209 |
|
| 210 | static void atreg_unregist_rsp_proc(unsigned char *aucDataBuf)
|
| 211 | {
|
| 212 | struct atreg_msg_t *patreg_msgdata = (struct atreg_msg_t *)aucDataBuf;
|
| 213 |
|
| 214 | switch (patreg_msgdata->type) {
|
| 215 | case AT_REG_SER:
|
| 216 | atreg_ser_unregist_rsp_proc(patreg_msgdata);
|
| 217 | break;
|
| 218 |
|
| 219 | case AT_REG_INFO:
|
| 220 | atreg_info_unregist_rsp_proc(patreg_msgdata);
|
| 221 | break;
|
| 222 |
|
| 223 | default:
|
| 224 | break;
|
| 225 | }
|
| 226 |
|
| 227 | sem_post(&atreg_common_ctx.sem_id);
|
| 228 |
|
| 229 | return;
|
| 230 | }
|
| 231 |
|
| 232 |
|
| 233 | static void atreg_ser_cb_proc(void *patreg_instance, unsigned char *aucDataBuf)
|
| 234 | {
|
| 235 | int send_ret = -1;
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 236 | unsigned char res_msg[MSG_DATA_MAX_LEN] = {0};
|
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 237 | struct atreg_ser_instance_t *patreg_ser_instance = (struct atreg_ser_instance_t *)patreg_instance;
|
| 238 |
|
| 239 | patreg_ser_instance->cb(aucDataBuf, res_msg);
|
| 240 |
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 241 | send_ret = ipc_send_message2(atreg_common_ctx.modid, MODULE_ID_AT_CTL, patreg_ser_instance->rsp_msg_id, strlen(res_msg), (unsigned char *)res_msg, 0);
|
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 242 | if (0 != send_ret) {
|
| 243 | slog(ATREG_PRINT, SLOG_ERR, "Err: atreg_ser_cb_proc ipc fail!\n");
|
| 244 | }
|
| 245 |
|
| 246 | return;
|
| 247 | }
|
| 248 |
|
| 249 |
|
| 250 | static void atreg_info_cb_proc(void *patreg_instance, unsigned char *aucDataBuf)
|
| 251 | {
|
| 252 | struct atreg_info_instance_t *patreg_info_instance = (struct atreg_info_instance_t *)patreg_instance;
|
| 253 |
|
| 254 | patreg_info_instance->cb(aucDataBuf);
|
| 255 |
|
| 256 | return;
|
| 257 | }
|
| 258 |
|
| 259 |
|
| 260 | static void atreg_cb_proc(int req_msg_id, unsigned char *aucDataBuf)
|
| 261 | {
|
| 262 | struct atreg_instance_and_type_t atreg_instance_and_type = {0};
|
| 263 |
|
| 264 | atreg_instance_and_type = atreg_search_instance_and_type_by_reqid(req_msg_id);
|
| 265 |
|
| 266 | switch (atreg_instance_and_type.type) {
|
| 267 | case AT_REG_SER:
|
| 268 | atreg_ser_cb_proc(atreg_instance_and_type.instance, aucDataBuf);
|
| 269 | break;
|
| 270 |
|
| 271 | case AT_REG_INFO:
|
| 272 | atreg_info_cb_proc(atreg_instance_and_type.instance, aucDataBuf);
|
| 273 | break;
|
| 274 |
|
| 275 | default:
|
| 276 | break;
|
| 277 | }
|
| 278 |
|
| 279 | return;
|
| 280 | }
|
| 281 |
|
| 282 |
|
| 283 | static void atreg_msg_proc(MSG_BUF *msg)
|
| 284 | {
|
| 285 | if(NULL == msg) {
|
| 286 | slog(ATREG_PRINT, SLOG_ERR, "Err: atreg_msg_proc the msg is NULL!\n");
|
| 287 | return;
|
| 288 | }
|
| 289 |
|
| 290 | switch(msg->usMsgCmd) {
|
| 291 | case MSG_CMD_AT_REG_RSP:
|
| 292 | atreg_regist_rsp_proc(msg->aucDataBuf);
|
| 293 | break;
|
| 294 |
|
| 295 | case MSG_CMD_AT_UNREG_RSP:
|
| 296 | atreg_unregist_rsp_proc(msg->aucDataBuf);
|
| 297 | break;
|
| 298 |
|
| 299 | default:
|
| 300 | atreg_cb_proc(msg->usMsgCmd, msg->aucDataBuf);
|
| 301 | break;
|
| 302 | }
|
| 303 |
|
| 304 | return;
|
| 305 | }
|
| 306 |
|
| 307 |
|
| 308 | /*******************************************************************************
|
| 309 | * Global function implementations *
|
| 310 | *******************************************************************************/
|
| 311 | /** ¸ÃÏ̴߳´½¨Ö®ºó£¬½«²»»áÍ˳ö */
|
| 312 | void atreg_msg_thread_entry(void *arg)
|
| 313 | {
|
| 314 | struct atreg_common_context_t *patreg_common_ctx = (struct atreg_common_context_t *)arg;
|
| 315 | int imsgq = -1;
|
| 316 | int iret = 0;
|
| 317 | MSG_BUF msgbuf;
|
| 318 | long msgsize = sizeof(MSG_BUF) - sizeof(long);
|
| 319 | char name[32] = {0};
|
| 320 |
|
| 321 | if(NULL == patreg_common_ctx) {
|
| 322 | slog(ATREG_PRINT, SLOG_ERR, "Err: at_thread_entry the arg is NULL!\n");
|
| 323 | return;
|
| 324 | }
|
| 325 |
|
| 326 | snprintf(name, 31, "atreg%d", patreg_common_ctx->modid);
|
| 327 | prctl(PR_SET_NAME, name, 0, 0, 0);
|
| 328 |
|
| 329 | /* ´´½¨atregÏûÏ¢¶ÓÁÐ */
|
| 330 | imsgq = msgget(patreg_common_ctx->modid, IPC_CREAT|0600);
|
| 331 | if (-1 == imsgq) {
|
| 332 | slog(ATREG_PRINT, SLOG_ERR, "Err: atreg_thread_entry msgget fail!\n");
|
| 333 | return;
|
| 334 | }
|
| 335 |
|
| 336 | while(1) {
|
| 337 | memset(&msgbuf, 0, sizeof(MSG_BUF));
|
| 338 |
|
| 339 | iret = msgrcv(imsgq, &msgbuf, msgsize, 0, 0);
|
| 340 | if (-1 == iret) {
|
| 341 | slog(ATREG_PRINT, SLOG_ERR, "Err: atreg_thread_entry msgrcv errno=%s\n", errno);
|
| 342 | continue;
|
| 343 | }
|
| 344 | atreg_msg_proc(&msgbuf);
|
| 345 | }
|
| 346 |
|
| 347 | slog(ATREG_PRINT, SLOG_NORMAL, "atreg_thread_entry the thread is stop!\n");
|
| 348 | }
|
| 349 |
|