summaryrefslogtreecommitdiff
path: root/enhanced-position-service/test/compliance-test/enhanced-position-service-compliance-test.c
blob: caac5b00c11b0c5164094054383ec73fc8c628f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**************************************************************************
* @licence app begin@
*
* SPDX-License-Identifier: MPL-2.0
*
* Component Name: EnhancedPositionService
* Author: Marco Residori <marco.residori@xse.de>
*
* Copyright (C) 2013, XS Embedded GmbH
* 
* License:
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
* this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Note:
* This test application was written using the code examples
* provided at http://www.matthew.ath.cx/misc/dbus as a reference.
*
* @licence end@
**************************************************************************/

#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define LAT 32  //value taken from GENIVI API
#define LON 33  //value taken from GENIVI API
#define ALT 34  //value taken from GENIVI API

const char * DBUS_INTERFACE = "org.genivi.positioning.EnhancedPosition";
const char * DBUS_PATH = "/position";
const char * DBUS_SOURCE = "org.genivi.positioning.Client";
const char * DBUS_DESTINATION = "org.genivi.positioning.EnhancedPosition";
const char * DBUS_METHOD_GET_DATA = "GetData";

/**
 * This method emulates the API method GetData
 */
bool getPositioningData() 
{
    DBusMessage* msg;
    DBusMessageIter args_iter;
    DBusMessageIter data_iter;
    DBusMessageIter msg_iter;
    DBusMessageIter value_iter;
    DBusMessageIter array_iter;
    DBusConnection* conn;
    DBusError err;
    DBusPendingCall* pending;
    double latitude;
    double longitude;
    double altitude; //should be of type int (the PoC will have to be corrected)
    int ret;

    //initialise the errors
    dbus_error_init(&err);

    //connect to the session bus and check for errors
    conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
    if(dbus_error_is_set(&err)) 
    { 
        fprintf(stderr, "connection error (%s)\n", err.message); 
        dbus_error_free(&err);
    }

    if(conn == NULL) 
    { 
        return false; 
    }

    //request name on the bus (optional)
    ret = dbus_bus_request_name(conn, DBUS_SOURCE, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
    if(dbus_error_is_set(&err)) 
    { 
        fprintf(stderr, "dbus name error (%s)\n", err.message); 
        dbus_error_free(&err);
    }

    if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) 
    { 
        return false; 
    }

    //create a new method call and check for errors
    msg = dbus_message_new_method_call(DBUS_DESTINATION, //destination
                                       DBUS_PATH, //object
                                       DBUS_INTERFACE, //interface
                                       DBUS_METHOD_GET_DATA); //method name
    if(NULL == msg) 
    { 
        fprintf(stderr, "message null\n");
        return false; 
    }

    dbus_uint16_t array[2];
    int size = 3;
    array[0]=LAT;
    array[1]=LON;
    array[2]=ALT;
    const dbus_uint16_t *v_ARRAY = array;

    //append arguments
    dbus_message_iter_init_append(msg, &args_iter);

    if(!dbus_message_iter_open_container(&args_iter, DBUS_TYPE_ARRAY,"q" , &data_iter))
    {
        fprintf(stderr, "out of memory!\n");
        return false; 
    }

    if(!dbus_message_iter_append_fixed_array(&data_iter, DBUS_TYPE_UINT16, &v_ARRAY, size))
    {
        fprintf(stderr, "out Of Memory!\n");
        return false; 
    }

    if(!dbus_message_iter_close_container(&args_iter, &data_iter))
    {
        fprintf(stderr, "out of memory!\n");
        return false; 
    }
 
    //send message and get a handle for a reply
    if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) // -1 is the default timeout
    { 
        fprintf(stderr, "out of memory!\n"); 
        return false; 
    }

    if(pending == NULL) 
    { 
        fprintf(stderr, "pending call is null\n"); 
        return false;  
    }
    dbus_connection_flush(conn);
   
    dbus_message_unref(msg);
   
    //block until we recieve a reply
    dbus_pending_call_block(pending);

    //get the reply message
    msg = dbus_pending_call_steal_reply(pending);
    if(msg == NULL) 
    {
        fprintf(stderr, "reply null\n"); 
        return false; 
    }

    //free the pending message handle
    dbus_pending_call_unref(pending);

    if(!dbus_message_iter_init(msg, &msg_iter)) 
    {
        fprintf(stderr, "format error\n");
        return false; 
    }

    //get the dictionary a{qv} 
    if (dbus_message_iter_get_arg_type (&msg_iter) != DBUS_TYPE_ARRAY ||
        dbus_message_iter_get_element_type (&msg_iter) !=
        DBUS_TYPE_DICT_ENTRY)
    {
        fprintf(stderr, "format error\n");
        return false; 
    }
 
    for (dbus_message_iter_recurse(&msg_iter, &array_iter);
         dbus_message_iter_get_arg_type (&array_iter) != DBUS_TYPE_INVALID;
         dbus_message_iter_next (&array_iter))
    {
        DBusMessageIter dict_iter;
        dbus_uint16_t key;
 
        //check that the array entry is a dict entry 
        if (dbus_message_iter_get_arg_type (&array_iter) != DBUS_TYPE_DICT_ENTRY)
        {
            return false; 
        }

        //recurse in dict entry  
        dbus_message_iter_recurse (&array_iter, &dict_iter);
 
        if(dbus_message_iter_get_arg_type(&dict_iter) != DBUS_TYPE_UINT16)
        {
  	        fprintf(stderr, "format error: wrong data type %c\n", (char)dbus_message_iter_get_arg_type(&dict_iter));
            return false;
        }
 
        dbus_message_iter_get_basic(&dict_iter, &key);
        dbus_message_iter_next(&dict_iter);
       
        switch(key)
        {
            case LAT:
            //read variant value 
            if (dbus_message_iter_get_arg_type (&dict_iter) == DBUS_TYPE_VARIANT)
            {
                dbus_message_iter_recurse(&dict_iter, &value_iter);

                if(dbus_message_iter_get_arg_type(&value_iter) != DBUS_TYPE_DOUBLE)
                {
                    fprintf(stderr, "format error: wrong data type %c\n", (char)dbus_message_iter_get_arg_type(&value_iter));
                    return false;
                }

                dbus_message_iter_get_basic(&value_iter, (void *)&latitude);
                printf("latitude=%f\n",latitude);

                if(latitude > 90 || latitude < -90)
                {
                   fprintf(stderr, "latitude value out of range\n");
                   return false;
                }
            }
            break;

            case LON:
            //read variant value 
            if (dbus_message_iter_get_arg_type (&dict_iter) == DBUS_TYPE_VARIANT)
            {
                dbus_message_iter_recurse(&dict_iter, &value_iter);

                if(dbus_message_iter_get_arg_type(&value_iter) != DBUS_TYPE_DOUBLE)
                {
                    fprintf(stderr, "format error: wrong data type %c\n", (char)dbus_message_iter_get_arg_type(&value_iter));
                    return false;
                }

                dbus_message_iter_get_basic(&value_iter, (void *)&longitude);
                printf("longitude=%f\n",longitude);

                if(longitude > 180 || longitude < -180)
                {
                    fprintf(stderr, "longitude value out of range\n");
                    return false;
                }
            }
            break;

            case ALT:
            //read following variant value 
            if(dbus_message_iter_get_arg_type (&dict_iter) == DBUS_TYPE_VARIANT)
            {
                dbus_message_iter_recurse(&dict_iter, &value_iter);

                if (dbus_message_iter_get_arg_type(&value_iter) != DBUS_TYPE_DOUBLE) //should be DBUS_TYPE_INT32
                {
                    fprintf(stderr, "format error: wrong data type %c\n", (char)dbus_message_iter_get_arg_type(&value_iter));
                    return false;
                }

                dbus_message_iter_get_basic(&value_iter, (void *)&altitude);
                printf("altitude=%f\n",altitude); //should be 'd' instead (the PoC will have to be corrected)

                if(altitude > 9000 || longitude < -1000)
                {
                    fprintf(stderr, "altitude value out of range\n");
                    return false;
                }
            }
            break;

            default:
                //do nothing
            break;
        }
    }
 
    //free reply and close connection
    dbus_message_unref(msg);   

    //dbus_connection_close(conn);

    return true;
}

int main(int argc, char** argv)
{
    printf("Starting gnss-service-compliance-test...\n");
  
    if(getPositioningData() == false)
    {
       printf("TEST_FAILED\n");
       return EXIT_FAILURE;
    }

    printf("TEST_PASSED\n");
    return EXIT_SUCCESS;
}