stable now, graphs might need some tweaks
[monky] / src / bme.c
1 /* 
2  * File:   bme.C
3  * Author: Lance
4  * 
5  * Created on March 6, 2012, 11:09 PM
6  */
7
8 //#include "bme.h"
9
10 /*
11 ** Copyright 2012, Alexey Roslyakov <alexey.roslyakov@newsycat.com>
12 **
13 ** Licensed under the Apache License, Version 2.0 (the "License");
14 ** you may not use this file except in compliance with the License.
15 ** You may obtain a copy of the License at
16 **
17 **     http://www.apache.org/licenses/LICENSE-2.0
18 **
19 ** Unless required by applicable law or agreed to in writing, software
20 ** distributed under the License is distributed on an "AS IS" BASIS,
21 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 ** See the License for the specific language governing permissions and
23 ** limitations under the License.
24 */
25
26 // from here: https://gitorious.org/harmattan-goodies/bmecli/blobs/master/bmecli.c
27
28 #include <unistd.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34
35 typedef unsigned int uint32;
36 typedef unsigned short uint16;
37 typedef unsigned char uint8;
38
39 //const char DefaultSocketPath[] = "/tmp/.bmesrv";
40
41 struct bme_reply { //22 items
42   uint32     unknown1;
43   uint32     unknown2;
44   uint32     charger_type; // 0 - none, 2 - usbmax500, 3 - usbwall
45   uint32     unknown4;
46   uint32     unknown5;
47   uint32     charging_time;
48   uint32     unknown7;
49   uint32     unknown8;
50   uint32     battery_max_level; // - cur level?
51   uint32     battery_cur_level; // /
52   uint32     unknown11;
53   uint32     unknown12;
54   uint32     battery_max_capacity;
55   uint32     battery_cur_capacity;
56   uint32     battery_max_voltage;
57   uint32     battery_cur_voltage;
58   uint32     battery_pct_level;
59   uint32     battery_temperature;
60   int        battery_current;
61   uint32     unknown20;
62   uint32     battery_last_full_capacity;
63   uint8      reserved[128];
64 };
65
66 static void dumpBuffer(const void *buf, size_t bufSize)
67 {
68   int cnt = 0; char *p = (char*)buf;
69   while (bufSize--)
70     fprintf(stderr, "%02X%s", *p++, (++cnt & 15 ? " " : "\n") );
71   fprintf(stderr, "\n");
72 }
73
74 const char* bmeClientGetData(const char *bmeSocketPath, struct bme_reply *bmeReply)
75 {
76   //static int x = 0;
77   //printf("%d\n", x); // outputs the value of x //always crashes on 23 or 24
78   //x = x + 1;
79     
80   int s = socket(AF_LOCAL, SOCK_STREAM, 0);
81   if (s <= 0)
82     return "socket";
83   struct sockaddr_un name;
84   name.sun_family = AF_LOCAL;
85   strcpy(name.sun_path, bmeSocketPath);
86   if ( connect(s, (struct sockaddr *)&name, sizeof(name)) != 0)
87     return "connect";
88
89   char foo[256];
90   size_t rd;
91   // TODO: clean up this crap
92   write(s, "SYNC\10\0\0\0BMentity", 16);
93   rd = read(s, foo, sizeof(foo));
94   //NORM_ERR("read %i bytes",rd); //reads 9 every time
95   //fprintf(stderr, "rd=%zu\n", rd);
96   write(s, "SYNC\4\0\0\0\3\200\0\0\n", 12);//crashes here
97  
98   rd = read(s, foo, 20);
99   //NORM_ERR("bme socket read %i bytes",rd); //reads -1 right before it crashes
100   //fprintf(stderr, "rd=%zu\n", rd);
101   rd = read(s, bmeReply, sizeof(*bmeReply));
102   //fprintf(stderr, "rd=%zu\n", rd);
103  
104   // FIXME: sometimes we get extra data in replies
105   //NORM_ERR("read %i bytes",rd); //reads 0 right before it crashes
106   if (rd > 128){
107     memmove( bmeReply, ((char*) bmeReply) + rd - 128, 128);
108   }
109  // dumpBuffer(bmeReply, rd);
110   close(s);
111   return 0;
112 }
113
114 //int print_usage(const char *prog)
115 //{
116 //  fprintf(stderr, "Usage: %s <bme_socket_path>\n", prog);
117 //  fprintf(stderr, "sizeof=%zu\n", sizeof (struct bme_reply));
118 //
119 //  return -1;
120 //}
121
122 struct bme_reply getBattInfoFromBME()
123 {
124 //  if (argc != 2)
125 //    return print_usage(argv[0]);
126
127   struct bme_reply bmeReply;
128   
129   memset(&bmeReply, 0, sizeof bmeReply);
130   //NORM_ERR("bme 1");
131   const char *failedOn = bmeClientGetData("/tmp/.bmesrv", &bmeReply);
132   //NORM_ERR("bme 2");
133   if (failedOn) {
134     NORM_ERR("bme_client_get_data failed on %s: %s\n", failedOn, strerror(errno));
135     //try again
136     failedOn = 0;
137     const char *failedOn = bmeClientGetData("/tmp/.bmesrv", &bmeReply);
138     if (failedOn){//still failed
139         NORM_ERR("failed 2x in a row @ bme");        
140         bmeReply.battery_temperature = -1;
141         bmeReply.battery_current = 0;
142         return bmeReply;
143     }
144   }
145
146
147 /*
148   fprintf(stderr, "BME stat\n");
149   fprintf(stderr, "Unknown 1,2,4,5: %u %u %u %u\n", bmeReply.unknown1, bmeReply.unknown2, bmeReply.unknown4, bmeReply.unknown5);
150   fprintf(stderr, "Charger type:\t\t%u\n", bmeReply.charger_type);
151   fprintf(stderr, "Charging time:\t\t%u\n", bmeReply.charging_time);
152   fprintf(stderr, "Battery temperature:\t%.2f\n", bmeReply.battery_temperature - 273.15f);
153   fprintf(stderr, "Battery max. Level:\t%u\n", bmeReply.battery_max_level);
154   fprintf(stderr, "Battery cur. Level:\t%u\n", bmeReply.battery_cur_level);
155   fprintf(stderr, "Battery pct. Level:\t%u\n", bmeReply.battery_pct_level);
156   fprintf(stderr, "Battery Max. Capacity:\t%u\n", bmeReply.battery_max_capacity);
157   fprintf(stderr, "Battery Cur. Capacity:\t%u\n", bmeReply.battery_cur_capacity);
158   fprintf(stderr, "Battery Last Full Cap:\t%u\n", bmeReply.battery_last_full_capacity);
159   fprintf(stderr, "Battery Max. Voltage:\t%u\n", bmeReply.battery_max_voltage);
160   fprintf(stderr, "Battery Cur. Voltage:\t%u\n", bmeReply.battery_cur_voltage);
161   fprintf(stderr, "Battery Current:\t%d\n", bmeReply.battery_current);
162   fprintf(stderr, "Unknown 7,8:\t%u %u\n", bmeReply.unknown7, bmeReply.unknown7);
163   fprintf(stderr, "Unknown 11,12:\t%u %u\n", bmeReply.unknown11, bmeReply.unknown12);
164   fprintf(stderr, "Unknown20 (timestamp?):\t%u\n", bmeReply.unknown20);
165   fprintf(stderr, "\n");  
166 */
167
168
169   return bmeReply;
170 }
171
172