-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.c
204 lines (165 loc) · 5.71 KB
/
main.c
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
/*
* Copyright 2022 WolkAbout Technology s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "utility/wolk_utils.h"
#include "wolk_connector.h"
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <openssl/ssl.h>
static SSL_CTX* ctx;
static BIO* sockfd;
/* WolkAbout Platform device connection parameters */
static const char* device_key = "device_key";
static const char* device_password = "some_password";
static const char* hostname = "insert_host";
static int portno = 80; // TODO: insert port
static char certs[] = "path/to/your/ca/crt/file";
/* Sample in-memory persistence storage - size 1MB */
static uint8_t persistence_storage[1024 * 1024];
/* WolkConnect-C Connector context */
static wolk_ctx_t wolk;
static volatile bool keep_running = true;
static void int_handler(int dummy)
{
WOLK_UNUSED(dummy);
keep_running = false;
}
static int send_buffer(unsigned char* buffer, unsigned int len)
{
int n;
n = (int)BIO_write(sockfd, buffer, (int)len);
if (n < 0) {
return -1;
}
return n;
}
static int receive_buffer(unsigned char* buffer, unsigned int max_bytes)
{
bzero(buffer, max_bytes);
int n;
n = (int)BIO_read(sockfd, buffer, (int)max_bytes);
if (n < 0) {
return -1;
}
return n;
}
static void open_socket(BIO** bio, SSL_CTX** ssl_ctx, const char* addr, const int port_number, const char* ca_file,
const char* ca_path)
{
SSL* ssl;
char port[5];
snprintf(port, 5, "%d", port_number);
/* Load OpenSSL */
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
SSL_library_init();
*ssl_ctx = SSL_CTX_new(SSLv23_client_method());
/* Load Certificate */
if (!SSL_CTX_load_verify_locations(*ssl_ctx, ca_file, ca_path)) {
printf("Wolk client - Error, failed to load certificate\n");
exit(1);
}
/* Open BIO Socket */
*bio = BIO_new_ssl_connect(*ssl_ctx);
BIO_get_ssl(*bio, &ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
BIO_set_conn_hostname(*bio, addr);
BIO_set_nbio(*bio, 1);
BIO_set_conn_port(*bio, port);
/* Wait for connection in 15 second timeout */
int start_time = (int)time(NULL);
while (BIO_do_connect(*bio) <= 0 && (int)time(NULL) - start_time < 15)
;
if (BIO_do_connect(*bio) <= 0) {
printf("Wolk client - Error, do connect\n");
BIO_free_all(*bio);
SSL_CTX_free(*ssl_ctx);
*bio = NULL;
*ssl_ctx = NULL;
return;
}
/* Verify Certificate */
if (SSL_get_verify_result(ssl) != X509_V_OK) {
printf("Wolk client - Error, x509 certificate verification failed\n");
exit(1);
}
}
int main(int argc, char* argv[])
{
WOLK_UNUSED(argc);
WOLK_UNUSED(argv);
signal(SIGINT, int_handler);
signal(SIGTERM, int_handler);
if (strcmp(device_key, "device_key") == 0) {
printf("Wolk client - Error, device key not provided\n");
return 1;
}
if (strcmp(device_password, "password") == 0) {
printf("Wolk client - Error, password key not provided\n");
return 1;
}
printf("Wolk client - Establishing connection to WolkAbout IoT platform\n");
open_socket(&sockfd, &ctx, hostname, portno, certs, NULL);
if (sockfd == NULL) {
printf("Wolk client - Error establishing connection to WolkAbout IoT platform\n");
return 1;
}
if (wolk_init(&wolk, send_buffer, receive_buffer, device_key, device_password, PUSH, NULL, NULL, NULL) != W_FALSE) {
printf("Wolk client - Error initializing WolkConnect-C\n");
return 1;
}
if (wolk_init_in_memory_persistence(&wolk, persistence_storage, sizeof(persistence_storage), false) != W_FALSE) {
printf("Wolk client - Error initializing in-memory persistence\n");
return 1;
}
printf("Wolk client - Connecting to server\n");
if (wolk_connect(&wolk) != W_FALSE) {
printf("Wolk client - Error connecting to server\n");
return -1;
}
printf("Wolk client - Connected to server\n");
int32_t tick_count = 0;
wolk_numeric_feeds_t feed = {0};
int32_t heartbeat = 60000; // 60 000ms == 1min
while (keep_running) {
// Sending random values for reference "T"
if (tick_count > heartbeat) {
tick_count = 0;
feed.value = rand() % 100 - 20;
if (wolk_add_numeric_feed(&wolk, "T", &feed, 1))
printf("Wolk client - Error serializing numeric feed!\n");
if (wolk_publish(&wolk))
printf("Wolk client - Error publishing data!\n");
else
printf("Wolk client - for feed reference T published value is %lf\n", feed.value);
}
// MANDATORY: keep looping for a new messages. Sleep(currently 1000us) and number of tick(currently 1)
// you can change this parameter, but keep it's multiplication
usleep(1000);
wolk_process(&wolk, 1);
tick_count++;
}
printf("Wolk client - Diconnecting\n");
wolk_disconnect(&wolk);
BIO_free_all(sockfd);
return 0;
}