nmxp.c 3.07 KB
Newer Older
Matteo Quintiliani's avatar
Start    
Matteo Quintiliani committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*! \file
 *
 * \brief Nanometrics Protocol Libray
 *
 * Author:
 * 	Matteo Quintiliani
 * 	Istituto Nazionale di Geofisica e Vulcanologia - Italy
 *	quintiliani@ingv.it
 *
 */

#include "nmxp.h"

#include <stdio.h>
15
#include <stdlib.h>
Matteo Quintiliani's avatar
Start    
Matteo Quintiliani committed
16
17
#include <string.h>

18
19
int nmxp_sendConnect(int isock) {
    return nmxp_sendMessage(isock, NMXPMSG_CONNECT, NULL, 0);
Matteo Quintiliani's avatar
Start    
Matteo Quintiliani committed
20
21
}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
22
int nmxp_sendTerminateSubscription(int isock, NMXP_REASON_SHUTDOWN reason, char *message) {
23
    return nmxp_sendMessage(isock, NMXPMSG_TERMINATESUBSCRIPTION, message, ((message)? strlen(message)-1 : 0));
Matteo Quintiliani's avatar
Start    
Matteo Quintiliani committed
24
25
}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
26
int nmxp_receiveChannelList(int isock, NMXP_CHAN_LIST **pchannelList) {
Matteo Quintiliani's avatar
Start    
Matteo Quintiliani committed
27
28
29
    int ret;
    int i;

Matteo Quintiliani's avatar
Matteo Quintiliani committed
30
    NMXP_MSG_SERVER type;
Matteo Quintiliani's avatar
Start    
Matteo Quintiliani committed
31
32
33
34
35
    void *buffer;
    uint32_t length;

    *pchannelList = NULL;

36
    ret = nmxp_receiveMessage(isock, &type, &buffer, &length);
Matteo Quintiliani's avatar
Start    
Matteo Quintiliani committed
37
38

    if(type != NMXPMSG_CHANNELLIST) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
39
	nmxp_log(1, 0, "Type %d is not NMXPMSG_CHANNELLIST!\n", type);
Matteo Quintiliani's avatar
Start    
Matteo Quintiliani committed
40
41
42
43
44
    } else {

	*pchannelList = buffer;
	(*pchannelList)->number = ntohl((*pchannelList)->number);

Matteo Quintiliani's avatar
Matteo Quintiliani committed
45
	nmxp_log(0, 1, "number of channels %d\n", (*pchannelList)->number);
Matteo Quintiliani's avatar
Start    
Matteo Quintiliani committed
46
47
48
49
50
	
	// TODO check

	for(i=0; i < (*pchannelList)->number; i++) {
	    (*pchannelList)->channel[i].key = ntohl((*pchannelList)->channel[i].key);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
51
	    nmxp_log(0, 1, "%d %s\n", (*pchannelList)->channel[i].key, (*pchannelList)->channel[i].name);
Matteo Quintiliani's avatar
Start    
Matteo Quintiliani committed
52
53
54
55
56
57
58
59
	}

    }

    return ret;
}


Matteo Quintiliani's avatar
Matteo Quintiliani committed
60
int nmxp_sendAddTimeSeriesChannel(int isock, NMXP_CHAN_LIST *channelList, uint32_t shortTermCompletion, uint32_t out_format, NMXP_BUFFER_FLAG buffer_flag) {
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
    int ret;
    uint32_t buffer_length = 16 + (4 * channelList->number); 
    char *buffer = malloc(buffer_length);
    uint32_t app, i, disp;

    disp=0;

    app = htonl(channelList->number);
    memcpy(&buffer[disp], &app, 4);
    disp+=4;

    for(i=0; i < channelList->number; i++) {
	app = htonl(channelList->channel[i].key);
	memcpy(&buffer[disp], &app, 4);
	disp+=4;
    }
    
    app = htonl(shortTermCompletion);
    memcpy(&buffer[disp], &app, 4);
    disp+=4;

    app = htonl(out_format);
    memcpy(&buffer[disp], &app, 4);
    disp+=4;

    app = htonl(buffer_flag);
    memcpy(&buffer[disp], &app, 4);
    disp+=4;

90
    ret = nmxp_sendMessage(isock, NMXPMSG_ADDTIMESERIESCHANNELS, buffer, buffer_length);
91
92
93
94
95
96
97
98

    if(buffer) {
	free(buffer);
    }
    return ret;
}


Matteo Quintiliani's avatar
Matteo Quintiliani committed
99
int nmxp_receiveCompressedData(int isock, NMXP_CHAN_LIST *channelList) {
100
101
    int ret;

Matteo Quintiliani's avatar
Matteo Quintiliani committed
102
    NMXP_MSG_SERVER type;
103
104
105
    void *buffer;
    uint32_t length;

106
    ret = nmxp_receiveMessage(isock, &type, &buffer, &length);
107
108

    if(type != NMXPMSG_COMPRESSED) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
109
	nmxp_log(1, 0, "Type %d is not NMXPMSG_COMPRESSED!\n", type);
110
111
    } else {

112
	nmxp_processCompressedData(buffer, length, channelList);
113
114
115
116
117
    }

    return ret;
}

118

Matteo Quintiliani's avatar
Matteo Quintiliani committed
119
int nmxp_receiveDecompressedData(int isock, NMXP_CHAN_LIST *channelList) {
120
121
    int ret;

Matteo Quintiliani's avatar
Matteo Quintiliani committed
122
    NMXP_MSG_SERVER type;
123
124
125
    void *buffer;
    uint32_t length;

126
    ret = nmxp_receiveMessage(isock, &type, &buffer, &length);
127
128

    if(type != NMXPMSG_DECOMPRESSED) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
129
	nmxp_log(1, 0, "Type %d is not NMXPMSG_DECOMPRESSED!\n", type);
130
131
    } else {

132
	nmxp_processDecompressedData(buffer, length, channelList);
133
134
135
136
137
    }

    return ret;
}