blob: 1ff7efac0609ac81c3fd33a0ed8613af87d85af2 [file] [log] [blame]
lichengzhang45091302023-10-08 02:10:34 -07001#include <string.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <error.h>
5#include <string.h>
6#include <errno.h>
7#include <liblog/lynq_deflog.h>
8#include "include/liblynq-at-common.h"
jb.qi539afe42023-11-23 01:52:18 -08009#include <include/lynq-qser-autosuspend.h>
rita6966c302024-01-09 10:50:18 +080010#include <libled/lynq_led.h>
lichengzhang45091302023-10-08 02:10:34 -070011
12DEFINE_LYNQ_LIB_LOG(LYNQ_AT_COMMON)
13
14lynq_atsvc_outcb handle_output;
15typedef struct
16{
17 char *cmd;
18 void (*func)(char *input);
19}Command;
20
21enum
22{
23 Response = 0,
24 Urc
25};
26
27void lynq_response_ok()
28{
lichengzhanga252bb82023-10-14 00:22:02 -070029 char *str = "OK\r\n";
lichengzhang45091302023-10-08 02:10:34 -070030 handle_output(str, strlen(str), Response);
31}
32
33void lynq_response_error(int error_code)
34{
35 char str[32] = {0};
lichengzhanga252bb82023-10-14 00:22:02 -070036 sprintf(str, "+CME ERROR: %d\r\n", error_code);
lichengzhang45091302023-10-08 02:10:34 -070037 handle_output(str, strlen(str), Response);
38}
39
40void lynq_handle_version()
41{
42 char buf[64] = {0};
lichengzhanga252bb82023-10-14 00:22:02 -070043 sprintf(buf,"+CGIR:%s\r\n",LYNQ_SW_INSIDE_VERSION);
lichengzhang45091302023-10-08 02:10:34 -070044 handle_output(buf, strlen(buf), Response);
45 lynq_response_ok();
46 return;
47}
48
jb.qi539afe42023-11-23 01:52:18 -080049void lynq_handle_autosuspend(char* input)
50{
51 int len;
52 int ret;
53 char buf[64] = {0};
54 ALOGE("lynq_handle_autosuspend start\n");
55 len = strlen(input);
jb.qi3763a822023-12-21 02:05:12 -080056 system("echo 0 >/sys/devices/platform/soc/1307000.gmac/gmac_power");
jb.qi539afe42023-11-23 01:52:18 -080057 ret = qser_autosuspend_enable(input[len-1]);
58 if(ret != 0)
59 {
60 sprintf(buf,"+CME ERROR: 100\r\n");
61 handle_output(buf, strlen(buf), Response);
62 }
63 else
64 {
65 lynq_response_ok();
66 }
67
68 return;
69}
70
rita6966c302024-01-09 10:50:18 +080071void lynq_handle_netled(char* input)
72{
73 int ret;
74 char buf[64] = {0};
75 int mode = input[strlen(input)-1]-'0';
76 ALOGE("lynq_handle_netled start\n");
77
78 ret = lynq_set_netled_on(mode);
79 if(ret != 0)
80 {
81 sprintf(buf,"+CME ERROR: 100\r\n");
82 handle_output(buf, strlen(buf), Response);
83 }
84 else
85 {
86 lynq_response_ok();
87 }
88
89 return;
90}
91
92void lynq_handle_statusled(char* input)
93{
94 int ret;
95 char buf[64] = {0};
96 int mode = input[strlen(input)-1]-'0';
97
98 ALOGE("lynq_handle_statusled start\n");
99 ret = lynq_set_statusled_on(mode);
100
101 if(ret != 0)
102 {
103 sprintf(buf,"+CME ERROR: 100\r\n");
104 handle_output(buf, strlen(buf), Response);
105 }
106 else
107 {
108 lynq_response_ok();
109 }
110
111 return;
112}
lichengzhang136c1842024-04-17 16:59:03 +0800113#define mem_addr_lenth 10
114void lynq_handle_ddr_identify()
115{
116 char buf[64] = {0};
117 char *command = "devmem 0x0121b040";
118 char result[64] = {0};
119 FILE *fp;
120
121 fp = popen(command, "r");
122 if(fp == NULL)
123 {
124 lynq_response_error(100);
125 return;
126 }
127 fgets(result, sizeof(result), fp);
128 pclose(fp);
129
130 if(strncmp(result,"0xF8631600",mem_addr_lenth) == 0 || strncmp(result,"0xF8631700",mem_addr_lenth) == 0)
131 {
132 sprintf(buf,"+DDRID:2Gb ddr\r\n");
133 handle_output(buf, strlen(buf), Response);
134 }
135 else if(strncmp(result,"0xF8631300",mem_addr_lenth) == 0 || strncmp(result,"0xF8631500",mem_addr_lenth) == 0)
136 {
137 sprintf(buf,"+DDRID:4Gb ddr\r\n");
138 handle_output(buf, strlen(buf), Response);
139 }
140 else
141 {
142 sprintf(buf,"+DDRID:no matching ddr\r\n");
143 handle_output(buf, strlen(buf), Response);
144 lynq_response_error(100);
145 return;
146 }
147 lynq_response_ok();
148 return;
149}
150
jb.qi539afe42023-11-23 01:52:18 -0800151static Command commands[] =
lichengzhang45091302023-10-08 02:10:34 -0700152{
153 {"CGIR",lynq_handle_version},
jb.qi539afe42023-11-23 01:52:18 -0800154 {"LEELSP",lynq_handle_autosuspend},
rita6966c302024-01-09 10:50:18 +0800155 {"NETLED",lynq_handle_netled},
156 {"STATUSLED",lynq_handle_statusled},
lichengzhang136c1842024-04-17 16:59:03 +0800157 {"DDRID",lynq_handle_ddr_identify},
lichengzhang45091302023-10-08 02:10:34 -0700158 {NULL, NULL}
159};
160
161Command* find_command(char *input)
162{
163 ALOGD("function %s line %d input %s\n", __FUNCTION__, __LINE__, input);
164 int i;
165 int ret = -1;
166 for (i = 0; commands[i].cmd; i++)
167 {
168 ret = strncmp(input, commands[i].cmd, strlen(commands[i].cmd));
169 if(ret == 0)
170 {
171 ALOGD("function %s line %d find input %s commands[i].cmd %s strlen %d ret %d\n", __FUNCTION__, __LINE__, input, commands[i].cmd, strlen(commands[i].cmd), ret);
172 return (&commands[i]);
173 }
174 }
175 ALOGD("function %s line %d not find ret %d \n", __FUNCTION__, __LINE__, ret);
176 return ((Command *)NULL);
177}
178
179void lynq_at_common_cb(char *input, int input_max_size)
180{
181 if(handle_output != NULL)
182 {
183 ALOGD("function %s line %d input %s\n", __FUNCTION__, __LINE__, input);
184 if(input != NULL)
185 {
186 char *handle_string = input + strlen("AT+");
187 ALOGE("HJAHS:%s\n",handle_string);
188 if(!strlen(handle_string))
189 {
190 ALOGE("function %s line %d strlen %d\n", __FUNCTION__, __LINE__, strlen(handle_string));
191 return;
192 }
193 ALOGD("function %s line %d handle_string %s\n", __FUNCTION__, __LINE__, handle_string);
194 Command *cmd = find_command(handle_string);
195 if(cmd != NULL)
196 {
197 ALOGD("function %s line %d\n", __FUNCTION__, __LINE__);
198 (*(cmd->func))(handle_string);
199 return;
200 }
201 }
202 }
203}
204
205lynq_atsvc_incb lynq_register_at_common(lynq_atsvc_outcb out_cb)
206{
207 if(out_cb != NULL)
208 {
209 handle_output = out_cb;
210 ALOGD("function %s line %d\n", __FUNCTION__, __LINE__);
211 return lynq_at_common_cb;
212 }
jb.qi539afe42023-11-23 01:52:18 -0800213}