summaryrefslogtreecommitdiff
path: root/sensors-service/src/sns-use-iphone.c
blob: 78d9c97933cbcae54d0e24349decd2ad0e2f104f (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
/**************************************************************************
* @licence app begin@
*
* SPDX-License-Identifier: MPL-2.0
*
* \ingroup SensorsService
* \author Marco Residori <marco.residori@xse.de>
*
* \copyright 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/.
*
* @licence end@
**************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <errno.h>
#include <pthread.h>
#include <assert.h>

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <memory.h>

#include "globals.h"
#include "sns-init.h"
#include "log.h"

#define BUFLEN 256
#define MSGIDLEN 20
#define PORT 5555

//Sensors IDs used by the SensorLogger App
#define GPS_SENSOR 1
#define ACC_SENSOR 2
#define COMPASS    3
#define GYRO       4

DLT_DECLARE_CONTEXT(gContext);

pthread_t listenerThread;
pthread_mutex_t mutexCb;
pthread_mutex_t mutexData;
bool isRunning = true;

void *listenForMessages( void *ptr );

bool snsInit()
{
    isRunning = true;

    if(pthread_create( &listenerThread, NULL, listenForMessages, NULL) != 0)
    {
        isRunning = false;

    	return false;
    }

    return true;
}

bool snsDestroy()
{
    isRunning = false;

    if(listenerThread)
    {
        pthread_join( listenerThread, NULL);
    }

    return true;
}

void snsGetVersion(int *major, int *minor, int *micro)
{
    if(major)
    {
        *major = GENIVI_SNS_API_MAJOR;
    }

    if(minor)
    {
        *minor = GENIVI_SNS_API_MINOR;
    }

    if(micro)
    {
        *micro = GENIVI_SNS_API_MICRO;
    }
}

static bool processGVGYRO(char* data, TGyroscopeData* pGyroscopeData)
{
    long unsigned int timestamp = 0;
    float yawRate;
    float pitchRate;
    float rollRate;
    int32_t sensorId;
    uint32_t n = 0;

    if(!data || !pGyroscopeData)
    {
        LOG_ERROR_MSG(gContext,"wrong parameter!");
        return false;
    }

    n = sscanf(data, "%lu,%d,%f,%f,%f", &timestamp, &sensorId, &yawRate, &pitchRate, &rollRate);

    if(n <= 0)
    {
        LOG_ERROR_MSG(gContext,"replayer: processGVGYRO failed!");
        return false;
    }

    pGyroscopeData->timestamp = timestamp;

    //LOG_INFO(gContext,"timestamp:%lu",timestamp);

    //angleYaw
    pGyroscopeData->yawRate = yawRate;
    pGyroscopeData->validityBits |= GYROSCOPE_CONFIG_ANGLEYAW_VALID;
    LOG_INFO(gContext,"yawRate: %lf", pGyroscopeData->yawRate);
     
    //anglePitch
    pGyroscopeData->pitchRate = pitchRate;
    pGyroscopeData->validityBits |= GYROSCOPE_CONFIG_ANGLEPITCH_VALID;
    LOG_INFO(gContext,"pitchRate: %lf", pGyroscopeData->pitchRate);

    //angleRoll
    pGyroscopeData->rollRate = rollRate;
    pGyroscopeData->validityBits |= GYROSCOPE_CONFIG_ANGLEROLL_VALID;
    LOG_INFO(gContext,"rollRate: %f", pGyroscopeData->rollRate);

    if(cbGyroscope != 0)
    {
        cbGyroscope(pGyroscopeData,1);
    }

    return true;
}

void *listenForMessages( void *ptr )
{  
    struct sockaddr_in si_me;
    struct sockaddr_in si_other;
    long unsigned int timestamp = 0;
    int s;
    socklen_t slen = sizeof(si_other);
    char buf[BUFLEN];
    int msgId;
    int port = PORT;

    DLT_REGISTER_APP("SNSS", "SENSORS-SERVICE");
    DLT_REGISTER_CONTEXT(gContext,"SSRV", "Global Context");

    LOG_INFO(gContext,"Listening on port %d...",port);

    if((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
    {
        LOG_ERROR_MSG(gContext,"socket() failed!");
        exit(EXIT_FAILURE);
    }

    memset((char *) &si_me, 0, sizeof(si_me));
    si_me.sin_family = AF_INET;

    si_me.sin_port = htons(port);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);
    if(bind(s, (struct sockaddr *)&si_me, sizeof(si_me)) == -1)
    {
        LOG_ERROR_MSG(gContext,"socket() failed!");
        exit(EXIT_FAILURE);
    }

    while(isRunning == true)
    {
        if(recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen) == -1)
        {
            LOG_ERROR_MSG(gContext,"recvfrom() failed!");
            exit(EXIT_FAILURE);
        }

        LOG_DEBUG_MSG(gContext,"------------------------------------------------");

        LOG_DEBUG(gContext,"Received Packet from %s:%d", 
                  inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));

        sscanf(buf,"%lu,%d,",&timestamp, &msgId);

        LOG_DEBUG_MSG(gContext,"------------------------------------------------");
        LOG_DEBUG(gContext,"msgID:%d",msgId);
        
        pthread_mutex_lock(&mutexData);

        if(msgId == GYRO) 
        {
            processGVGYRO(buf, &gGyroscopeData);
        }

        pthread_mutex_unlock(&mutexData);
    }

    close(s);

    return EXIT_SUCCESS;
}