9792c3677a36e4d731ab7af5f2b0e107f6ec9b58
1 /**
2 * Copyright (C) ARM Limited 2010-2012. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <string.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include "Sender.h"
17 #include "Logging.h"
18 #include "SessionData.h"
20 extern void handleException();
22 Sender::Sender(OlySocket* socket) {
23 dataFile = NULL;
24 dataSocket = NULL;
26 // Set up the socket connection
27 if (socket) {
28 char streamline[64] = {0};
29 dataSocket = socket;
31 // Receive magic sequence - can wait forever
32 // Streamline will send data prior to the magic sequence for legacy support, which should be ignored for v4+
33 while (strcmp("STREAMLINE", streamline) != 0) {
34 if (dataSocket->receiveString(streamline, sizeof(streamline)) == -1) {
35 logg->logError(__FILE__, __LINE__, "Socket disconnected");
36 handleException();
37 }
38 }
40 // Send magic sequence - must be done first, afterwhich error messages can be sent
41 char magic[] = {'G', 'A', 'T', 'O', 'R', '\n'};
42 dataSocket->send(magic, sizeof(magic));
44 gSessionData->mWaitingOnCommand = true;
45 logg->logMessage("Completed magic sequence");
46 }
48 pthread_mutex_init(&sendMutex, NULL);
49 }
51 Sender::~Sender() {
52 delete dataSocket;
53 dataSocket = NULL;
54 if (dataFile) {
55 fclose(dataFile);
56 }
57 }
59 void Sender::createDataFile(char* apcDir) {
60 if (apcDir == NULL)
61 return;
63 dataFileName = (char*)malloc(strlen(apcDir) + 12);
64 sprintf(dataFileName, "%s/0000000000", apcDir);
65 dataFile = fopen(dataFileName, "wb");
66 if (!dataFile) {
67 logg->logError(__FILE__, __LINE__, "Failed to open binary file: %s", dataFileName);
68 handleException();
69 }
70 }
72 void Sender::writeData(const char* data, int length, int type) {
73 if (length < 0 || (data == NULL && length > 0)) {
74 return;
75 }
77 // Multiple threads call writeData()
78 pthread_mutex_lock(&sendMutex);
80 // Send data over the socket connection
81 if (dataSocket) {
82 // Start alarm
83 alarm(8);
85 // Send data over the socket, sending the type and size first
86 logg->logMessage("Sending data with length %d", length);
87 dataSocket->send((char*)&type, 1);
88 dataSocket->send((char*)&length, sizeof(length));
89 dataSocket->send((char*)data, length);
91 // Stop alarm
92 alarm(0);
93 }
95 // Write data to disk as long as it is not meta data
96 if (dataFile && type == RESPONSE_APC_DATA) {
97 logg->logMessage("Writing data with length %d", length);
98 // Send data to the data file, storing the size first
99 if ((fwrite((char*)&length, 1, sizeof(length), dataFile) != sizeof(length)) || (fwrite(data, 1, length, dataFile) != (unsigned int)length)) {
100 logg->logError(__FILE__, __LINE__, "Failed writing binary file %s", dataFileName);
101 handleException();
102 }
103 }
105 pthread_mutex_unlock(&sendMutex);
106 }