c0d21535aacf2f03ce5bfae6681b0a8237c1c805
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / Examples / common / atcodec_client.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6
7 #ifndef AT_MESSAGES_HEADER
8 # define AT_MESSAGES_HEADER <ATcodec/ATcodec_Messages_ex.h>
9 #endif // > AT_MESSAGES_HEADER
10
11
12 #include <ATcodec/ATcodec_api.h>
13 #include <VP_Os/vp_os_types.h>
14 #include <VP_Com/vp_com.h>
15 #include <VP_Api/vp_api_error.h>
16 #include <VP_Os/vp_os_signal.h>
17 #include <VP_Os/vp_os_delay.h>
18 #include <VP_Os/vp_os_thread.h>
19 #include <VP_Os/vp_os_print.h>
20 #include <VP_Os/vp_os_malloc.h>
21 #include <Examples/common/common.h>
22 #include <Examples/common/atcodec_client.h>
23 #include <VP_Stages/vp_stages_io_com.h>
24
25
26 #ifndef AT_MESSAGES_HEADER
27 #error You need to define AT_MESSAGES_HEADER
28 #endif
29
30
31 AT_CODEC_MSG_IDS ids;
32
33 static vp_stages_input_com_config_t icc;
34 static Write atcodec_write;
35 static Read atcodec_read;
36
37 static vp_com_t com;
38
39 #ifdef NO_COM
40   static vp_com_serial_config_t          config;
41 #else // ! NO_COM
42 #ifdef USE_WIFI
43   static vp_com_wifi_connection_t        connection;
44   static vp_com_wifi_config_t            config;
45 #endif // ! USE_WIFI
46 #endif // > NO_COM
47
48 AT_CODEC_ERROR_CODE atresu_fake(ATcodec_Memory_t *mem, ATcodec_Memory_t *output, int *id)
49 {
50   PRINT("FAKE received !\n");
51   return AT_CODEC_GENERAL_OK;
52 }
53
54 AT_CODEC_ERROR_CODE atresu_ok(ATcodec_Memory_t *mem, ATcodec_Memory_t *output, int *id)
55 {
56   PRINT("OK received !\n");
57   return AT_CODEC_GENERAL_OK;
58 }
59
60 AT_CODEC_ERROR_CODE atresu_error(ATcodec_Memory_t *mem, ATcodec_Memory_t *output, int *id)
61 {
62   PRINT("ERROR received !\n");
63   return AT_CODEC_GENERAL_OK;
64 }
65
66 AT_CODEC_ERROR_CODE atresu_cgmi(ATcodec_Memory_t *mem, ATcodec_Memory_t *output, int *id)
67 {
68   char str[1024];
69
70   //int len =
71   ATcodec_Memory_Raw_Get_Int(mem);
72
73   ATcodec_Memory_Get_String(mem, &str[0]);
74
75   PRINT("CGMI received : \"%s\" !\n", &str[0]);
76
77   return AT_CODEC_GENERAL_OK;
78 }
79
80 AT_CODEC_ERROR_CODE AT_CODEC_Client_init(void)
81 {
82   static int foo = 1;
83
84   vp_os_memset(&com, 0, sizeof(vp_com_t));
85   icc.com = &com;
86
87   if(foo)
88     {
89
90 # undef ATCODEC_DEFINE_AT_CMD
91 # define ATCODEC_DEFINE_AT_CMD(ID,Str,From,Cb,Prio) \
92     if((ids.ID = ATcodec_Add_Defined_Message(Str)) == -1) \
93       { \
94         fprintf(stderr, "Error Add_Hashed \"%s\" library\n", Str); \
95         return AT_CODEC_INIT_ERROR; \
96       }
97
98 # undef ATCODEC_DEFINE_AT_RESU
99 # define ATCODEC_DEFINE_AT_RESU(ID,Str,From,Cb) \
100     if((ids.ID = ATcodec_Add_Hashed_Message(Str,ids.From,Cb,0)) == -1) \
101       { \
102         fprintf(stderr, "Error Add_Defined \"%s\" library\n", Str); \
103         return AT_CODEC_INIT_ERROR; \
104       }
105
106 # include AT_MESSAGES_HEADER
107
108   //ATcodec_Print_Tree();
109
110   //vp_delay(100);
111
112 #ifdef NO_COM
113 #ifdef __linux__
114   strcpy(config.itfName, "/dev/ttyUSB0");
115 #else
116   strcpy(config.itfName, "/dev/ser0");
117 #endif
118   config.initial_baudrate = VP_COM_BAUDRATE_115200;
119   config.baudrate = VP_COM_BAUDRATE_115200;
120   config.sync = 0; //1;
121
122   com.type                = VP_COM_SERIAL;
123   icc.socket.type         = VP_COM_CLIENT;
124   icc.config              = (vp_com_config_t *)&config;
125   icc.buffer_size         = 16;
126 #else // ! NO_COM
127 #ifdef USE_WIFI
128   strcpy(connection.networkName,"linksys");
129
130   strcpy(config.itfName,    "rausb0");
131   strcpy(config.localHost,  "192.168.1.57");
132   strcpy(config.netmask,    "255.255.255.0");
133   strcpy(config.broadcast,  "192.168.1.255");
134   strcpy(config.gateway,    "192.168.1.1");
135   strcpy(config.server,     "192.168.1.1");
136   strcpy(config.passkey,    "9F1C3EE11CBA230B27BF1C1B6F");
137   config.infrastructure       = 1;
138   config.secure               = 1;
139
140   com.type                          = VP_COM_WIFI;
141   icc.config                        = (vp_com_config_t*)&config;
142   icc.connection                    = (vp_com_connection_t*)&connection;
143   icc.socket.type                   = VP_COM_CLIENT;
144   icc.socket.protocol               = VP_COM_TCP;
145   icc.socket.port                   = 5555;
146   icc.buffer_size                   = 6400;
147
148   strcpy(icc.socket_client.serverHost,"192.168.1.23");
149 #endif // ! USE_WIFI
150 #endif // > NO_COM
151
152   if(FAILED(vp_com_init(icc.com)))
153   {
154     PRINT("VP_Com : Failed to init\n");
155     vp_com_shutdown(icc.com);
156     return AT_CODEC_OPEN_ERROR;
157   }
158
159   if(FAILED(vp_com_local_config(icc.com, icc.config)))
160   {
161     PRINT("VP_Com : Failed to configure\n");
162     vp_com_shutdown(icc.com);
163     return AT_CODEC_OPEN_ERROR;
164   }
165
166   PRINT("com_init OK\n");
167
168   foo = 0;
169
170     }
171
172   return AT_CODEC_INIT_OK;
173 }
174
175 AT_CODEC_ERROR_CODE AT_CODEC_Client_shutdown(void)
176 {
177   ATcodec_Shutdown_Library();
178
179   return AT_CODEC_SHUTDOWN_OK;
180 }
181
182 int rfcomm_socket;
183
184 AT_CODEC_ERROR_CODE AT_CODEC_Client_open(void)
185 {
186   static int foo = 1;
187
188   printf("ATcodec_open\n");
189
190   if(foo)
191     {
192 /*   if(FAILED(vp_com_connect(icc.connection, 1))) */
193 /*     return AT_CODEC_OPEN_ERROR; */
194
195   if(FAILED(vp_com_open(icc.com, &icc.socket_client, &atcodec_read,&atcodec_write)))
196     return AT_CODEC_OPEN_ERROR;
197
198   foo = 0;
199
200     }
201
202   return AT_CODEC_OPEN_OK;
203 }
204
205 AT_CODEC_ERROR_CODE AT_CODEC_Client_close(void)
206 {
207   printf("ATcodec_close\n");
208
209   vp_com_close(icc.com, &icc.socket_client);
210
211   return AT_CODEC_CLOSE_OK;
212 }
213
214 AT_CODEC_ERROR_CODE AT_CODEC_Client_write(int8_t *buffer, int32_t *len)
215 {
216   //printf("ATcodec_write\n");
217
218   if(FAILED(atcodec_write(&icc.socket_client, buffer, len)))
219      return AT_CODEC_WRITE_ERROR;
220
221   return AT_CODEC_WRITE_OK;
222 }
223
224 AT_CODEC_ERROR_CODE AT_CODEC_Client_read(int8_t *buffer, int32_t *len)
225 {
226 /*   printf("< ATcodec_read >\n"); */
227
228   if(FAILED(atcodec_read(&icc.socket_client, buffer, len)))
229        return AT_CODEC_READ_ERROR;
230
231   if(*len)
232     {
233   buffer[*len] = '\0';
234 /*   PRINT("len=%d AT_CODEC_Client_read : \"", *len); */
235 /*   for( ; *buffer ; buffer++) */
236 /*     { */
237 /*       if(*buffer == '\r') */
238 /*      { */
239 /*        PRINT("<CR>"); */
240 /*      } */
241 /*       else if(*buffer == '\n') */
242 /*      { */
243 /*        PRINT("<LF>"); */
244 /*      } */
245 /*       else if(*buffer == '\0') */
246 /*      { */
247 /*        PRINT("<\\0>"); */
248 /*      } */
249 /*       else */
250 /*      { */
251 /*        PRINT("%c", *buffer); */
252 /*      } */
253 /*     } */
254 /*   PRINT("\"\n"); */
255     }
256
257   return AT_CODEC_READ_OK;
258 }