ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / Examples / Android / ardrone / project / jni / app.c
1 /*
2  * AR Drone demo
3  *
4  * originally based on Android NDK "San Angeles" demo app
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include <pthread.h>
14 #include <sys/time.h>
15 #include <time.h>
16 #include <android/log.h>
17
18 #include "app.h"
19 #include "control_ack.h"
20 #include "video_stage.h"
21
22 #include <ardrone_api.h>
23 #include <ardrone_tool/ardrone_tool.h>
24 #include <ardrone_tool/Control/ardrone_control.h>
25 #include <ardrone_tool/Navdata/ardrone_navdata_client.h>
26 #include <ardrone_tool/ardrone_tool.>
27 #include <VP_Api/vp_api_thread_helper.h>
28
29 char drone_ip[24];
30 int gAppAlive   = 0;
31
32 // assume drone has address x.x.x.1
33 #define WIFI_ARDRONE_ADDR_LAST_BYTE (1)
34
35 #define DEBUG_THREAD    1
36 static bool_t bContinue = TRUE;
37
38 void get_drone_ip(void)
39 {
40     int ret;
41     char prop_value[PROP_VALUE_MAX];
42     char prop_name[PROP_NAME_MAX];
43     int inet_part[4];
44
45     // quick and dirty way to guess Drone IP address
46     ret = GETPROP("wifi.interface", prop_value);
47     if (ret > 0) {
48         INFO("wifi interface = %s\n", prop_value);
49         snprintf(prop_name, PROP_NAME_MAX, "dhcp.%s.ipaddress", prop_value);
50         ret = GETPROP(prop_name, prop_value);
51     }
52     if (ret > 0) {
53         INFO("IP address = %s\n", prop_value);
54         ret = 0;
55         if (sscanf(prop_value, "%d.%d.%d.%d",
56                    &inet_part[0],
57                    &inet_part[1],
58                    &inet_part[2],
59                    &inet_part[3]) == 4) {
60             ret = 1;
61             sprintf(drone_ip, "%u.%u.%u.%u",
62                     inet_part[0]& 0xff, inet_part[1]& 0xff, inet_part[2]&0xff,
63                     WIFI_ARDRONE_ADDR_LAST_BYTE);
64         }
65     }
66     if (ret == 0) {
67         // fallback to default address
68         sprintf(drone_ip, WIFI_ARDRONE_IP);
69     }
70
71     INFO("assuming drone IP address is %s\n", drone_ip);
72 }
73
74 PROTO_THREAD_ROUTINE(mobile_main, data);
75 DEFINE_THREAD_ROUTINE(mobile_main, data)
76 {
77         __android_log_print( ANDROID_LOG_INFO, "ARDrone", "Enter in mobile_main thread\n" );
78         C_RESULT res = C_OK;
79         char drone_address[24];
80
81         vp_os_memset(drone_address, 0x0, sizeof(drone_address));
82         sprintf(drone_address, WIFI_ARDRONE_IP);
83
84         res = ardrone_tool_setup_com( NULL );
85
86         if( FAILED(res) )
87         {
88                 __android_log_print( ANDROID_LOG_INFO, "ARDrone", "Setup com failed\n" );
89                 INFO("Wifi initialization failed. It means either:\n");
90                 INFO("\t* you're not root (it's mandatory because you can set up wifi connection only as root)\n");
91                 INFO("\t* wifi device is not present (on your pc or on your card)\n");
92                 INFO("\t* you set the wrong name for wifi interface (for example rausb0 instead of wlan0) \n");
93                 INFO("\t* ap is not up (reboot card or remove wifi usb dongle)\n");
94                 INFO("\t* wifi device has no antenna\n");
95         }
96         else
97         {
98                 START_THREAD(video_stage, NULL);
99
100                 __android_log_print( ANDROID_LOG_INFO, "ARDrone", "Processing ardrone_tool_init\n" );
101                 res = ardrone_tool_init(drone_address, strlen(drone_address), NULL);
102
103       control_ack_init();
104       control_ack_configure_navdata_demo( FALSE );
105
106                 if(SUCCEED(res))
107                 {   
108                         res = ardrone_tool_set_refresh_time(25);
109
110                         while( SUCCEED(res) && bContinue == TRUE )
111                         {
112                                 res = ardrone_tool_update();
113                         }
114                 }
115
116                 JOIN_THREAD(video_stage);
117
118                 res = ardrone_tool_shutdown();
119         }
120
121         __android_log_print( ANDROID_LOG_INFO, "ARDrone", "Exit mobile_main thread\n" );
122         return (THREAD_RET)0;
123 }
124
125 void appInit(void)
126 {
127    __android_log_print( ANDROID_LOG_INFO, "ARDrone", "Enter in appInit\n" );
128         
129    // video rendering
130         video_init();
131 //    int status;
132
133   //  get_drone_ip();
134
135         gAppAlive = 1;
136         START_THREAD( mobile_main, NULL);
137 }
138
139 void appDeinit()
140 {
141     gAppAlive = 0;
142
143     INFO("shutting down application...\n");
144     
145     video_deinit();
146     JOIN_THREAD( mobile_main );
147 }
148
149 void appRender(long tick, int width, int height)
150 {
151     if (!gAppAlive) {
152         return;
153     }
154     video_render(tick, width, height);
155 }
156
157 BEGIN_THREAD_TABLE
158 THREAD_TABLE_ENTRY(mobile_main, 20)
159 THREAD_TABLE_ENTRY(ardrone_control, 20)
160 THREAD_TABLE_ENTRY(navdata_update, 20)
161 THREAD_TABLE_ENTRY(video_stage, 20)
162 END_THREAD_TABLE
163