xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /**
|
| 2 | * @file atreg_common.c
|
| 3 | * @brief Implementation of the inter APIs of libatreg.
|
| 4 | *
|
| 5 | * Copyright (C) 2021 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 |
|
| 22 | #include "atreg_common.h"
|
| 23 | #include "at_utils.h"
|
| 24 |
|
| 25 |
|
| 26 | /*******************************************************************************
|
| 27 | * Macro definitions *
|
| 28 | *******************************************************************************/
|
| 29 |
|
| 30 |
|
| 31 | /*******************************************************************************
|
| 32 | * Type definitions *
|
| 33 | *******************************************************************************/
|
| 34 |
|
| 35 |
|
| 36 | /*******************************************************************************
|
| 37 | * Local variable definitions *
|
| 38 | *******************************************************************************/
|
| 39 |
|
| 40 |
|
| 41 | /*******************************************************************************
|
| 42 | * Global variable definitions *
|
| 43 | *******************************************************************************/
|
| 44 |
|
| 45 | struct atreg_ser_context_t atreg_ser_ctx;
|
| 46 | struct atreg_info_context_t atreg_info_ctx;
|
| 47 | struct atreg_common_context_t atreg_common_ctx;
|
| 48 |
|
| 49 | /** ser - ¶¯Ì¬id³Ø, ·¶Î§: 0~511 */
|
| 50 | unsigned char atreg_ser_dynamic_idpool[ATREG_SER_ID_MAX];
|
| 51 | /** info - ¶¯Ì¬id³Ø, ·¶Î§: 512~768 */
|
| 52 | unsigned char atreg_info_dynamic_idpool[ATREG_INFO_ID_MAX];
|
| 53 |
|
| 54 | extern int atreg_ser_cxt_is_init;
|
| 55 | extern int atreg_info_cxt_is_init;
|
| 56 | /*******************************************************************************
|
| 57 | * Local function declarations *
|
| 58 | *******************************************************************************/
|
| 59 |
|
| 60 |
|
| 61 | /*******************************************************************************
|
| 62 | * Local function implementations *
|
| 63 | *******************************************************************************/
|
| 64 | static void *atreg_ser_search_instance_by_prefix_proc(char *at_cmd_prefix)
|
| 65 | {
|
| 66 | struct atreg_ser_instance_t * entry = NULL;
|
| 67 |
|
| 68 | pthread_mutex_lock(&atreg_ser_ctx.at_ser_lock);
|
| 69 | list_for_each_entry(entry, &atreg_ser_ctx.at_ser_list, list) {
|
| 70 | if(0 == at_strncmp(at_cmd_prefix, entry->at_cmd_prefix, AT_CMD_PREFIX)) {
|
| 71 | pthread_mutex_unlock(&atreg_ser_ctx.at_ser_lock);
|
| 72 |
|
| 73 | return (void *)entry;
|
| 74 | }
|
| 75 | }
|
| 76 | pthread_mutex_unlock(&atreg_ser_ctx.at_ser_lock);
|
| 77 |
|
| 78 | return NULL;
|
| 79 | }
|
| 80 |
|
| 81 |
|
| 82 | static void *atreg_info_search_instance_by_prefix_proc(char *at_cmd_prefix)
|
| 83 | {
|
| 84 | struct atreg_info_instance_t * entry = NULL;
|
| 85 |
|
| 86 | pthread_mutex_lock(&atreg_info_ctx.at_info_lock);
|
| 87 | list_for_each_entry(entry, &atreg_info_ctx.at_info_list, list) {
|
| 88 | if(0 == at_strncmp(at_cmd_prefix, entry->at_cmd_prefix, AT_CMD_PREFIX)) {
|
| 89 | pthread_mutex_unlock(&atreg_info_ctx.at_info_lock);
|
| 90 |
|
| 91 | return (void *)entry;
|
| 92 | }
|
| 93 | }
|
| 94 | pthread_mutex_unlock(&atreg_info_ctx.at_info_lock);
|
| 95 |
|
| 96 | return NULL;
|
| 97 | }
|
| 98 |
|
| 99 |
|
| 100 | static void *atreg_ser_search_instance_by_reqid_proc(int req_msg_id)
|
| 101 | {
|
| 102 | struct atreg_ser_instance_t *entry = NULL;
|
| 103 |
|
| 104 | pthread_mutex_lock(&atreg_ser_ctx.at_ser_lock);
|
| 105 | list_for_each_entry(entry, &atreg_ser_ctx.at_ser_list, list) {
|
| 106 | if(entry->req_msg_id == req_msg_id) {
|
| 107 | pthread_mutex_unlock(&atreg_ser_ctx.at_ser_lock);
|
| 108 |
|
| 109 | return (void *)entry;
|
| 110 | }
|
| 111 | }
|
| 112 | pthread_mutex_unlock(&atreg_ser_ctx.at_ser_lock);
|
| 113 |
|
| 114 | return NULL;
|
| 115 | }
|
| 116 |
|
| 117 |
|
| 118 | static void *atreg_info_search_instance_by_reqid_proc(int req_msg_id)
|
| 119 | {
|
| 120 | struct atreg_info_instance_t *entry = NULL;
|
| 121 |
|
| 122 | pthread_mutex_lock(&atreg_info_ctx.at_info_lock);
|
| 123 | list_for_each_entry(entry, &atreg_info_ctx.at_info_list, list) {
|
| 124 | if(entry->req_msg_id == req_msg_id) {
|
| 125 | pthread_mutex_unlock(&atreg_info_ctx.at_info_lock);
|
| 126 |
|
| 127 | return (void *)entry;
|
| 128 | }
|
| 129 | }
|
| 130 | pthread_mutex_unlock(&atreg_info_ctx.at_info_lock);
|
| 131 |
|
| 132 | return NULL;
|
| 133 | }
|
| 134 |
|
| 135 |
|
| 136 | static void *atreg_ser_search_instance_tmp_by_reqid_proc(int req_msg_id)
|
| 137 | {
|
| 138 | struct atreg_ser_instance_t *entry = NULL;
|
| 139 |
|
| 140 | pthread_mutex_lock(&atreg_ser_ctx.at_ser_lock_tmp);
|
| 141 | list_for_each_entry(entry, &atreg_ser_ctx.at_ser_list_tmp, list) {
|
| 142 | if(entry->req_msg_id == req_msg_id) {
|
| 143 | pthread_mutex_unlock(&atreg_ser_ctx.at_ser_lock_tmp);
|
| 144 |
|
| 145 | return (void *)entry;
|
| 146 | }
|
| 147 | }
|
| 148 | pthread_mutex_unlock(&atreg_ser_ctx.at_ser_lock_tmp);
|
| 149 |
|
| 150 | return NULL;
|
| 151 | }
|
| 152 |
|
| 153 |
|
| 154 | static void *atreg_info_search_instance_tmp_by_reqid_proc(int req_msg_id)
|
| 155 | {
|
| 156 | struct atreg_info_instance_t *entry = NULL;
|
| 157 |
|
| 158 | pthread_mutex_lock(&atreg_info_ctx.at_info_lock_tmp);
|
| 159 | list_for_each_entry(entry, &atreg_info_ctx.at_info_list_tmp, list) {
|
| 160 | if(entry->req_msg_id == req_msg_id) {
|
| 161 | pthread_mutex_unlock(&atreg_info_ctx.at_info_lock_tmp);
|
| 162 |
|
| 163 | return (void *)entry;
|
| 164 | }
|
| 165 | }
|
| 166 | pthread_mutex_unlock(&atreg_info_ctx.at_info_lock_tmp);
|
| 167 |
|
| 168 | return NULL;
|
| 169 | }
|
| 170 |
|
| 171 |
|
| 172 | /*******************************************************************************
|
| 173 | * Global function implementations *
|
| 174 | *******************************************************************************/
|
| 175 | void atreg_wait_rsp(int msg_cmd)
|
| 176 | {
|
| 177 | int ret = -1;
|
| 178 |
|
| 179 | if (clock_gettime(CLOCK_REALTIME, &atreg_common_ctx.ts) == -1) {
|
| 180 | slog(ATREG_PRINT, SLOG_NORMAL, "atreg_wait_rsp clock_gettime fail.\n");
|
| 181 | return;
|
| 182 | }
|
| 183 | atreg_common_ctx.ts.tv_sec += WAIT_RSP_TIMEOUT;
|
| 184 |
|
| 185 | while ((ret = sem_timedwait(&atreg_common_ctx.sem_id, &atreg_common_ctx.ts)) == -1 && errno == EINTR)
|
| 186 | continue;
|
| 187 | if(-1 == ret) {
|
| 188 | slog(ATREG_PRINT, SLOG_ERR, "Err: atreg_wait_rsp wait msg(%x) timeout.\n", msg_cmd);
|
| 189 | }
|
| 190 | else {
|
| 191 | slog(ATREG_PRINT, SLOG_NORMAL, "atreg_wait_rsp post msg(%x) response.\n", msg_cmd);
|
| 192 | }
|
| 193 |
|
| 194 | return;
|
| 195 | }
|
| 196 |
|
| 197 |
|
| 198 | void *atreg_search_instance_by_prefix(char *at_cmd_prefix, int atreg_type)
|
| 199 | {
|
| 200 | void* patreg = NULL;
|
| 201 |
|
| 202 | switch (atreg_type) {
|
| 203 | case AT_REG_SER:
|
| 204 | patreg = atreg_ser_search_instance_by_prefix_proc(at_cmd_prefix);
|
| 205 | break;
|
| 206 |
|
| 207 | case AT_REG_INFO:
|
| 208 | patreg = atreg_info_search_instance_by_prefix_proc(at_cmd_prefix);
|
| 209 | break;
|
| 210 |
|
| 211 | default:
|
| 212 | break;
|
| 213 | }
|
| 214 |
|
| 215 | return patreg;
|
| 216 | }
|
| 217 |
|
| 218 |
|
| 219 | void *atreg_search_instance_tmp_by_reqid(int req_msg_id, int atreg_type)
|
| 220 | {
|
| 221 | void* patreg = NULL;
|
| 222 |
|
| 223 | switch (atreg_type) {
|
| 224 | case AT_REG_SER:
|
| 225 | patreg = atreg_ser_search_instance_tmp_by_reqid_proc(req_msg_id);
|
| 226 | break;
|
| 227 |
|
| 228 | case AT_REG_INFO:
|
| 229 | patreg = atreg_info_search_instance_tmp_by_reqid_proc(req_msg_id);
|
| 230 | break;
|
| 231 |
|
| 232 | default:
|
| 233 | break;
|
| 234 | }
|
| 235 |
|
| 236 | return patreg;
|
| 237 | }
|
| 238 |
|
| 239 |
|
| 240 | struct atreg_instance_and_type_t atreg_search_instance_and_type_by_reqid(int req_msg_id)
|
| 241 | {
|
| 242 | void* patreg = NULL;
|
| 243 | struct atreg_instance_and_type_t atreg_instance_and_type = {0};
|
| 244 |
|
| 245 | if(atreg_ser_cxt_is_init)
|
| 246 | patreg = atreg_ser_search_instance_by_reqid_proc(req_msg_id);
|
| 247 | if (NULL != patreg) {
|
| 248 | atreg_instance_and_type.type = AT_REG_SER;
|
| 249 | atreg_instance_and_type.instance = patreg;
|
| 250 |
|
| 251 | return atreg_instance_and_type;
|
| 252 | }
|
| 253 | if(atreg_info_cxt_is_init)
|
| 254 | patreg = atreg_info_search_instance_by_reqid_proc(req_msg_id);
|
| 255 | if (NULL != patreg) {
|
| 256 | atreg_instance_and_type.type = AT_REG_INFO;
|
| 257 | atreg_instance_and_type.instance = patreg;
|
| 258 |
|
| 259 | return atreg_instance_and_type;
|
| 260 | }
|
| 261 | atreg_instance_and_type.type = -1;
|
| 262 | return atreg_instance_and_type;
|
| 263 | }
|
| 264 |
|