api.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. /*
  2. * Copyright 2011-2012 Andrew Smith
  3. * Copyright 2011-2012 Con Kolivas
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 2 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18. #include "compat.h"
  19. #include "miner.h"
  20. #if defined(unix) || defined(__APPLE__)
  21. #include <errno.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #define SOCKETTYPE int
  26. #define SOCKETFAIL(a) ((a) < 0)
  27. #define INVSOCK -1
  28. #define INVINETADDR -1
  29. #define CLOSESOCKET close
  30. #define SOCKERRMSG strerror(errno)
  31. #endif
  32. #ifdef WIN32
  33. #include <winsock2.h>
  34. #define SOCKETTYPE SOCKET
  35. #define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
  36. #define INVSOCK INVALID_SOCKET
  37. #define INVINETADDR INADDR_NONE
  38. #define CLOSESOCKET closesocket
  39. static char WSAbuf[1024];
  40. struct WSAERRORS {
  41. int id;
  42. char *code;
  43. } WSAErrors[] = {
  44. { 0, "No error" },
  45. { WSAEINTR, "Interrupted system call" },
  46. { WSAEBADF, "Bad file number" },
  47. { WSAEACCES, "Permission denied" },
  48. { WSAEFAULT, "Bad address" },
  49. { WSAEINVAL, "Invalid argument" },
  50. { WSAEMFILE, "Too many open sockets" },
  51. { WSAEWOULDBLOCK, "Operation would block" },
  52. { WSAEINPROGRESS, "Operation now in progress" },
  53. { WSAEALREADY, "Operation already in progress" },
  54. { WSAENOTSOCK, "Socket operation on non-socket" },
  55. { WSAEDESTADDRREQ, "Destination address required" },
  56. { WSAEMSGSIZE, "Message too long" },
  57. { WSAEPROTOTYPE, "Protocol wrong type for socket" },
  58. { WSAENOPROTOOPT, "Bad protocol option" },
  59. { WSAEPROTONOSUPPORT, "Protocol not supported" },
  60. { WSAESOCKTNOSUPPORT, "Socket type not supported" },
  61. { WSAEOPNOTSUPP, "Operation not supported on socket" },
  62. { WSAEPFNOSUPPORT, "Protocol family not supported" },
  63. { WSAEAFNOSUPPORT, "Address family not supported" },
  64. { WSAEADDRINUSE, "Address already in use" },
  65. { WSAEADDRNOTAVAIL, "Can't assign requested address" },
  66. { WSAENETDOWN, "Network is down" },
  67. { WSAENETUNREACH, "Network is unreachable" },
  68. { WSAENETRESET, "Net connection reset" },
  69. { WSAECONNABORTED, "Software caused connection abort" },
  70. { WSAECONNRESET, "Connection reset by peer" },
  71. { WSAENOBUFS, "No buffer space available" },
  72. { WSAEISCONN, "Socket is already connected" },
  73. { WSAENOTCONN, "Socket is not connected" },
  74. { WSAESHUTDOWN, "Can't send after socket shutdown" },
  75. { WSAETOOMANYREFS, "Too many references, can't splice" },
  76. { WSAETIMEDOUT, "Connection timed out" },
  77. { WSAECONNREFUSED, "Connection refused" },
  78. { WSAELOOP, "Too many levels of symbolic links" },
  79. { WSAENAMETOOLONG, "File name too long" },
  80. { WSAEHOSTDOWN, "Host is down" },
  81. { WSAEHOSTUNREACH, "No route to host" },
  82. { WSAENOTEMPTY, "Directory not empty" },
  83. { WSAEPROCLIM, "Too many processes" },
  84. { WSAEUSERS, "Too many users" },
  85. { WSAEDQUOT, "Disc quota exceeded" },
  86. { WSAESTALE, "Stale NFS file handle" },
  87. { WSAEREMOTE, "Too many levels of remote in path" },
  88. { WSASYSNOTREADY, "Network system is unavailable" },
  89. { WSAVERNOTSUPPORTED, "Winsock version out of range" },
  90. { WSANOTINITIALISED, "WSAStartup not yet called" },
  91. { WSAEDISCON, "Graceful shutdown in progress" },
  92. { WSAHOST_NOT_FOUND, "Host not found" },
  93. { WSANO_DATA, "No host data of that type was found" },
  94. { -1, "Unknown error code" }
  95. };
  96. static char *WSAErrorMsg()
  97. {
  98. char *msg;
  99. int i;
  100. int id = WSAGetLastError();
  101. /* Assume none of them are actually -1 */
  102. for (i = 0; WSAErrors[i].id != -1; i++)
  103. if (WSAErrors[i].id == id)
  104. break;
  105. sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
  106. return &(WSAbuf[0]);
  107. }
  108. #define SOCKERRMSG WSAErrorMsg()
  109. #ifndef SHUT_RDWR
  110. #define SHUT_RDWR SD_BOTH
  111. #endif
  112. #endif
  113. // Big enough for largest API request
  114. // though a PC with 100s of CPUs may exceed the size ...
  115. // Current code assumes it can socket send this size also
  116. #define MYBUFSIZ 32768
  117. // Number of requests to queue - normally would be small
  118. #define QUEUE 10
  119. static char *io_buffer = NULL;
  120. static char *msg_buffer = NULL;
  121. static SOCKETTYPE sock = INVSOCK;
  122. static const char *UNAVAILABLE = " - API will not be available";
  123. //static const char *BLANK = "";
  124. static const char *COMMA = ",";
  125. static const char SEPARATOR = '|';
  126. static const char GPUSEP = ',';
  127. static const char *APIVERSION = "1.0";
  128. static const char *DEAD = "Dead";
  129. static const char *SICK = "Sick";
  130. static const char *NOSTART = "NoStart";
  131. static const char *DISABLED = "Disabled";
  132. static const char *ALIVE = "Alive";
  133. #define _DYNAMIC "D"
  134. static const char *DYNAMIC = _DYNAMIC;
  135. static const char *YES = "Y";
  136. static const char *NO = "N";
  137. #define _DEVS "DEVS"
  138. #define _POOLS "POOLS"
  139. #define _SUMMARY "SUMMARY"
  140. #define _STATUS "STATUS"
  141. #define _VERSION "VERSION"
  142. #define _MINECON "CONFIG"
  143. #ifdef WANT_CPUMINE
  144. #define _CPU "CPU"
  145. #endif
  146. #define _GPU "GPU"
  147. #define _CPUS "CPUS"
  148. #define _GPUS "GPUS"
  149. #define _BYE "BYE"
  150. static const char ISJSON = '{';
  151. #define JSON0 "{"
  152. #define JSON1 "\""
  153. #define JSON2 "\":["
  154. #define JSON3 "]"
  155. #define JSON4 ",\"id\":1}"
  156. #define JSON_START JSON0
  157. #define JSON_DEVS JSON1 _DEVS JSON2
  158. #define JSON_POOLS JSON1 _POOLS JSON2
  159. #define JSON_SUMMARY JSON1 _SUMMARY JSON2
  160. #define JSON_STATUS JSON1 _STATUS JSON2
  161. #define JSON_VERSION JSON1 _VERSION JSON2
  162. #define JSON_MINECON JSON1 _MINECON JSON2
  163. #define JSON_GPU JSON1 _GPU JSON2
  164. #ifdef WANT_CPUMINE
  165. #define JSON_CPU JSON1 _CPU JSON2
  166. #endif
  167. #define JSON_GPUS JSON1 _GPUS JSON2
  168. #define JSON_CPUS JSON1 _CPUS JSON2
  169. #define JSON_BYE JSON1 _BYE JSON1
  170. #define JSON_CLOSE JSON3
  171. #define JSON_END JSON4
  172. static const char *JSON_COMMAND = "command";
  173. static const char *JSON_PARAMETER = "parameter";
  174. #define MSG_INVGPU 1
  175. #define MSG_ALRENA 2
  176. #define MSG_ALRDIS 3
  177. #define MSG_GPUMRE 4
  178. #define MSG_GPUREN 5
  179. #define MSG_GPUNON 6
  180. #define MSG_POOL 7
  181. #define MSG_NOPOOL 8
  182. #define MSG_DEVS 9
  183. #define MSG_NODEVS 10
  184. #define MSG_SUMM 11
  185. #define MSG_GPUDIS 12
  186. #define MSG_GPUREI 13
  187. #define MSG_INVCMD 14
  188. #define MSG_MISID 15
  189. #define MSG_GPUDEV 17
  190. #ifdef WANT_CPUMINE
  191. #define MSG_CPUNON 16
  192. #define MSG_CPUDEV 18
  193. #define MSG_INVCPU 19
  194. #endif
  195. #define MSG_NUMGPU 20
  196. #define MSG_NUMCPU 21
  197. #define MSG_VERSION 22
  198. #define MSG_INVJSON 23
  199. #define MSG_MISCMD 24
  200. #define MSG_MISPID 25
  201. #define MSG_INVPID 26
  202. #define MSG_SWITCHP 27
  203. #define MSG_MISVAL 28
  204. #define MSG_NOADL 29
  205. #define MSG_NOGPUADL 30
  206. #define MSG_INVINT 31
  207. #define MSG_GPUINT 32
  208. #define MSG_MINECON 33
  209. #define MSG_GPUMERR 34
  210. #define MSG_GPUMEM 35
  211. #define MSG_GPUEERR 36
  212. #define MSG_GPUENG 37
  213. #define MSG_GPUVERR 38
  214. #define MSG_GPUVDDC 39
  215. #define MSG_GPUFERR 40
  216. #define MSG_GPUFAN 41
  217. #define MSG_MISFN 42
  218. #define MSG_BADFN 43
  219. #define MSG_SAVED 44
  220. enum code_severity {
  221. SEVERITY_ERR,
  222. SEVERITY_WARN,
  223. SEVERITY_INFO,
  224. SEVERITY_SUCC,
  225. SEVERITY_FAIL
  226. };
  227. enum code_parameters {
  228. PARAM_GPU,
  229. PARAM_CPU,
  230. PARAM_GPUMAX,
  231. PARAM_CPUMAX,
  232. PARAM_PMAX,
  233. PARAM_POOLMAX,
  234. #ifdef WANT_CPUMINE
  235. PARAM_GCMAX,
  236. #else
  237. PARAM_GMAX,
  238. #endif
  239. PARAM_CMD,
  240. PARAM_POOL,
  241. PARAM_STR,
  242. PARAM_BOTH,
  243. PARAM_NONE
  244. };
  245. struct CODES {
  246. const enum code_severity severity;
  247. const int code;
  248. const enum code_parameters params;
  249. const char *description;
  250. } codes[] = {
  251. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  252. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  253. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  254. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  255. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  256. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  257. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  258. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  259. #ifdef WANT_CPUMINE
  260. { SEVERITY_SUCC, MSG_DEVS, PARAM_GCMAX, "%d GPU(s) - %d CPU(s)" },
  261. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs/CPUs" },
  262. #else
  263. { SEVERITY_SUCC, MSG_DEVS, PARAM_GMAX, "%d GPU(s)" },
  264. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs" },
  265. #endif
  266. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  267. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  268. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  269. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  270. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  271. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  272. #ifdef WANT_CPUMINE
  273. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  274. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  275. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  276. #endif
  277. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  278. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  279. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "CGMiner versions" },
  280. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  281. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  282. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  283. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  284. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  285. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  286. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  287. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  288. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range -10 - 10" },
  289. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  290. { SEVERITY_SUCC, MSG_MINECON, PARAM_NONE, "CGMiner config" },
  291. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  292. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported succeess" },
  293. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  294. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported succeess" },
  295. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  296. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported succeess" },
  297. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  298. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported succeess" },
  299. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  300. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  301. { SEVERITY_ERR, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  302. { SEVERITY_FAIL }
  303. };
  304. static int bye = 0;
  305. static bool ping = true;
  306. // All replies (except BYE) start with a message
  307. // thus for JSON, message() inserts JSON_START at the front
  308. // and send_result() adds JSON_END at the end
  309. static char *message(int messageid, int paramid, char *param2, bool isjson)
  310. {
  311. char severity;
  312. char *ptr;
  313. #ifdef WANT_CPUMINE
  314. int cpu;
  315. #endif
  316. int i;
  317. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  318. if (codes[i].code == messageid) {
  319. switch (codes[i].severity) {
  320. case SEVERITY_WARN:
  321. severity = 'W';
  322. break;
  323. case SEVERITY_INFO:
  324. severity = 'I';
  325. break;
  326. case SEVERITY_SUCC:
  327. severity = 'S';
  328. break;
  329. case SEVERITY_ERR:
  330. default:
  331. severity = 'E';
  332. break;
  333. }
  334. if (isjson)
  335. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"Code\":%d,\"Msg\":\"", severity, messageid);
  336. else
  337. sprintf(msg_buffer, _STATUS "=%c,Code=%d,Msg=", severity, messageid);
  338. ptr = msg_buffer + strlen(msg_buffer);
  339. switch(codes[i].params) {
  340. case PARAM_GPU:
  341. case PARAM_CPU:
  342. sprintf(ptr, codes[i].description, paramid);
  343. break;
  344. case PARAM_POOL:
  345. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  346. break;
  347. case PARAM_GPUMAX:
  348. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  349. break;
  350. case PARAM_PMAX:
  351. sprintf(ptr, codes[i].description, total_pools);
  352. break;
  353. case PARAM_POOLMAX:
  354. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  355. break;
  356. #ifdef WANT_CPUMINE
  357. case PARAM_GCMAX:
  358. if (opt_n_threads > 0)
  359. cpu = num_processors;
  360. else
  361. cpu = 0;
  362. sprintf(ptr, codes[i].description, nDevs, cpu);
  363. break;
  364. #else
  365. case PARAM_GMAX:
  366. sprintf(ptr, codes[i].description, nDevs);
  367. break;
  368. #endif
  369. case PARAM_CMD:
  370. sprintf(ptr, codes[i].description, JSON_COMMAND);
  371. break;
  372. case PARAM_STR:
  373. sprintf(ptr, codes[i].description, param2);
  374. break;
  375. case PARAM_BOTH:
  376. sprintf(ptr, codes[i].description, paramid, param2);
  377. break;
  378. case PARAM_NONE:
  379. default:
  380. strcpy(ptr, codes[i].description);
  381. }
  382. ptr = msg_buffer + strlen(msg_buffer);
  383. if (isjson)
  384. sprintf(ptr, "\",\"Description\":\"%s\"}" JSON_CLOSE, opt_api_description);
  385. else
  386. sprintf(ptr, ",Description=%s%c", opt_api_description, SEPARATOR);
  387. return msg_buffer;
  388. }
  389. }
  390. if (isjson)
  391. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE,
  392. messageid, opt_api_description);
  393. else
  394. sprintf(msg_buffer, _STATUS "=F,Code=-1,Msg=%d,Description=%s%c",
  395. messageid, opt_api_description, SEPARATOR);
  396. return msg_buffer;
  397. }
  398. static void apiversion(SOCKETTYPE c, char *param, bool isjson)
  399. {
  400. if (isjson)
  401. sprintf(io_buffer, "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE,
  402. message(MSG_VERSION, 0, NULL, isjson),
  403. VERSION, APIVERSION);
  404. else
  405. sprintf(io_buffer, "%s" _VERSION ",CGMiner=%s,API=%s%c",
  406. message(MSG_VERSION, 0, NULL, isjson),
  407. VERSION, APIVERSION, SEPARATOR);
  408. }
  409. static void minerconfig(SOCKETTYPE c, char *param, bool isjson)
  410. {
  411. char buf[BUFSIZ];
  412. int cpucount = 0;
  413. char *adlinuse = (char *)NO;
  414. #ifdef HAVE_ADL
  415. const char *adl = YES;
  416. int i;
  417. for (i = 0; i < nDevs; i++) {
  418. if (gpus[i].has_adl) {
  419. adlinuse = (char *)YES;
  420. break;
  421. }
  422. }
  423. #else
  424. const char *adl = NO;
  425. #endif
  426. #ifdef WANT_CPUMINE
  427. cpucount = opt_n_threads > 0 ? num_processors : 0;
  428. #endif
  429. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  430. if (isjson)
  431. sprintf(buf, "," JSON_MINECON "{\"GPU Count\":%d,\"CPU Count\":%d,\"Pool Count\":%d,\"ADL\":\"%s\",\"ADL in use\":\"%s\",\"Strategy\":\"%s\"}" JSON_CLOSE, nDevs, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s);
  432. else
  433. sprintf(buf, _MINECON ",GPU Count=%d,CPU Count=%d,Pool Count=%d,ADL=%s,ADL in use=%s,Strategy=%s%c", nDevs, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s, SEPARATOR);
  434. strcat(io_buffer, buf);
  435. }
  436. static void gpustatus(int gpu, bool isjson)
  437. {
  438. char intensity[20];
  439. char buf[BUFSIZ];
  440. char *enabled;
  441. char *status;
  442. float gt, gv;
  443. int ga, gf, gp, gc, gm, pt;
  444. if (gpu >= 0 && gpu < nDevs) {
  445. struct cgpu_info *cgpu = &gpus[gpu];
  446. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  447. #ifdef HAVE_ADL
  448. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  449. #endif
  450. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  451. if (cgpu->enabled)
  452. enabled = (char *)YES;
  453. else
  454. enabled = (char *)NO;
  455. if (cgpu->status == LIFE_DEAD)
  456. status = (char *)DEAD;
  457. else if (cgpu->status == LIFE_SICK)
  458. status = (char *)SICK;
  459. else if (cgpu->status == LIFE_NOSTART)
  460. status = (char *)NOSTART;
  461. else
  462. status = (char *)ALIVE;
  463. if (cgpu->dynamic)
  464. strcpy(intensity, DYNAMIC);
  465. else
  466. sprintf(intensity, "%d", gpus->intensity);
  467. if (isjson)
  468. sprintf(buf, "{\"GPU\":%d,\"Enabled\":\"%s\",\"Status\":\"%s\",\"Temperature\":%.2f,\"Fan Speed\":%d,\"Fan Percent\":%d,\"GPU Clock\":%d,\"Memory Clock\":%d,\"GPU Voltage\":%.3f,\"GPU Activity\":%d,\"Powertune\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Intensity\":\"%s\"}",
  469. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  470. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  471. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  472. cgpu->utility, intensity);
  473. else
  474. sprintf(buf, "GPU=%d,Enabled=%s,Status=%s,Temperature=%.2f,Fan Speed=%d,Fan Percent=%d,GPU Clock=%d,Memory Clock=%d,GPU Voltage=%.3f,GPU Activity=%d,Powertune=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Intensity=%s%c",
  475. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  476. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  477. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  478. cgpu->utility, intensity, SEPARATOR);
  479. strcat(io_buffer, buf);
  480. }
  481. }
  482. #ifdef WANT_CPUMINE
  483. static void cpustatus(int cpu, bool isjson)
  484. {
  485. char buf[BUFSIZ];
  486. if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
  487. struct cgpu_info *cgpu = &cpus[cpu];
  488. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  489. if (isjson)
  490. sprintf(buf, "{\"CPU\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Utility\":%.2f}",
  491. cpu, cgpu->total_mhashes / total_secs,
  492. opt_log_interval, cgpu->rolling,
  493. cgpu->accepted, cgpu->rejected,
  494. cgpu->utility);
  495. else
  496. sprintf(buf, "CPU=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Utility=%.2f%c",
  497. cpu, cgpu->total_mhashes / total_secs,
  498. opt_log_interval, cgpu->rolling,
  499. cgpu->accepted, cgpu->rejected,
  500. cgpu->utility, SEPARATOR);
  501. strcat(io_buffer, buf);
  502. }
  503. }
  504. #endif
  505. static void devstatus(SOCKETTYPE c, char *param, bool isjson)
  506. {
  507. int i;
  508. if (nDevs == 0 && opt_n_threads == 0) {
  509. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  510. return;
  511. }
  512. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  513. if (isjson) {
  514. strcat(io_buffer, COMMA);
  515. strcat(io_buffer, JSON_DEVS);
  516. }
  517. for (i = 0; i < nDevs; i++) {
  518. if (isjson && i > 0)
  519. strcat(io_buffer, COMMA);
  520. gpustatus(i, isjson);
  521. }
  522. #ifdef WANT_CPUMINE
  523. if (opt_n_threads > 0)
  524. for (i = 0; i < num_processors; i++) {
  525. if (isjson && (i > 0 || nDevs > 0))
  526. strcat(io_buffer, COMMA);
  527. cpustatus(i, isjson);
  528. }
  529. #endif
  530. if (isjson)
  531. strcat(io_buffer, JSON_CLOSE);
  532. }
  533. static void gpudev(SOCKETTYPE c, char *param, bool isjson)
  534. {
  535. int id;
  536. if (nDevs == 0) {
  537. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  538. return;
  539. }
  540. if (param == NULL || *param == '\0') {
  541. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  542. return;
  543. }
  544. id = atoi(param);
  545. if (id < 0 || id >= nDevs) {
  546. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  547. return;
  548. }
  549. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  550. if (isjson) {
  551. strcat(io_buffer, COMMA);
  552. strcat(io_buffer, JSON_GPU);
  553. }
  554. gpustatus(id, isjson);
  555. if (isjson)
  556. strcat(io_buffer, JSON_CLOSE);
  557. }
  558. #ifdef WANT_CPUMINE
  559. static void cpudev(SOCKETTYPE c, char *param, bool isjson)
  560. {
  561. int id;
  562. if (opt_n_threads == 0) {
  563. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  564. return;
  565. }
  566. if (param == NULL || *param == '\0') {
  567. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  568. return;
  569. }
  570. id = atoi(param);
  571. if (id < 0 || id >= num_processors) {
  572. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  573. return;
  574. }
  575. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  576. if (isjson) {
  577. strcat(io_buffer, COMMA);
  578. strcat(io_buffer, JSON_CPU);
  579. }
  580. cpustatus(id, isjson);
  581. if (isjson)
  582. strcat(io_buffer, JSON_CLOSE);
  583. }
  584. #endif
  585. static void poolstatus(SOCKETTYPE c, char *param, bool isjson)
  586. {
  587. char buf[BUFSIZ];
  588. char *status, *lp;
  589. int i;
  590. if (total_pools == 0) {
  591. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  592. return;
  593. }
  594. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  595. if (isjson) {
  596. strcat(io_buffer, COMMA);
  597. strcat(io_buffer, JSON_POOLS);
  598. }
  599. for (i = 0; i < total_pools; i++) {
  600. struct pool *pool = pools[i];
  601. if (!pool->enabled)
  602. status = (char *)DISABLED;
  603. else {
  604. if (pool->idle)
  605. status = (char *)DEAD;
  606. else
  607. status = (char *)ALIVE;
  608. }
  609. if (pool->hdr_path)
  610. lp = (char *)YES;
  611. else
  612. lp = (char *)NO;
  613. if (isjson)
  614. sprintf(buf, "%s{\"POOL\":%d,\"URL\":\"%s\",\"Status\":\"%s\",\"Priority\":%d,\"Long Poll\":\"%s\",\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Remote Failures\":%d}",
  615. (i > 0) ? COMMA : "",
  616. i, pool->rpc_url, status, pool->prio, lp,
  617. pool->getwork_requested,
  618. pool->accepted, pool->rejected,
  619. pool->discarded_work,
  620. pool->stale_shares,
  621. pool->getfail_occasions,
  622. pool->remotefail_occasions);
  623. else
  624. sprintf(buf, "POOL=%d,URL=%s,Status=%s,Priority=%d,Long Poll=%s,Getworks=%d,Accepted=%d,Rejected=%d,Discarded=%d,Stale=%d,Get Failures=%d,Remote Failures=%d%c",
  625. i, pool->rpc_url, status, pool->prio, lp,
  626. pool->getwork_requested,
  627. pool->accepted, pool->rejected,
  628. pool->discarded_work,
  629. pool->stale_shares,
  630. pool->getfail_occasions,
  631. pool->remotefail_occasions, SEPARATOR);
  632. strcat(io_buffer, buf);
  633. }
  634. if (isjson)
  635. strcat(io_buffer, JSON_CLOSE);
  636. }
  637. static void summary(SOCKETTYPE c, char *param, bool isjson)
  638. {
  639. double utility, mhs;
  640. #ifdef WANT_CPUMINE
  641. char *algo = (char *)(algo_names[opt_algo]);
  642. if (algo == NULL)
  643. algo = "(null)";
  644. #endif
  645. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  646. mhs = total_mhashes_done / total_secs;
  647. #ifdef WANT_CPUMINE
  648. if (isjson)
  649. sprintf(io_buffer, "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"Algorithm\":\"%s\",\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u}" JSON_CLOSE,
  650. message(MSG_SUMM, 0, NULL, isjson),
  651. total_secs, algo, mhs, found_blocks,
  652. total_getworks, total_accepted, total_rejected,
  653. hw_errors, utility, total_discarded, total_stale,
  654. total_go, local_work, total_ro, new_blocks);
  655. else
  656. sprintf(io_buffer, "%s" _SUMMARY ",Elapsed=%.0f,Algorithm=%s,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u%c",
  657. message(MSG_SUMM, 0, NULL, isjson),
  658. total_secs, algo, mhs, found_blocks,
  659. total_getworks, total_accepted, total_rejected,
  660. hw_errors, utility, total_discarded, total_stale,
  661. total_go, local_work, total_ro, new_blocks, SEPARATOR);
  662. #else
  663. if (isjson)
  664. sprintf(io_buffer, "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u}" JSON_CLOSE,
  665. message(MSG_SUMM, 0, NULL, isjson),
  666. total_secs, mhs, found_blocks,
  667. total_getworks, total_accepted, total_rejected,
  668. hw_errors, utility, total_discarded, total_stale,
  669. total_go, local_work, total_ro, new_blocks);
  670. else
  671. sprintf(io_buffer, "%s" _SUMMARY ",Elapsed=%.0f,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u%c",
  672. message(MSG_SUMM, 0, NULL, isjson),
  673. total_secs, mhs, found_blocks,
  674. total_getworks, total_accepted, total_rejected,
  675. hw_errors, utility, total_discarded, total_stale,
  676. total_go, local_work, total_ro, new_blocks, SEPARATOR);
  677. #endif
  678. }
  679. static void gpuenable(SOCKETTYPE c, char *param, bool isjson)
  680. {
  681. struct thr_info *thr;
  682. int gpu;
  683. int id;
  684. int i;
  685. if (gpu_threads == 0) {
  686. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  687. return;
  688. }
  689. if (param == NULL || *param == '\0') {
  690. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  691. return;
  692. }
  693. id = atoi(param);
  694. if (id < 0 || id >= nDevs) {
  695. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  696. return;
  697. }
  698. if (gpus[id].enabled) {
  699. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  700. return;
  701. }
  702. for (i = 0; i < gpu_threads; i++) {
  703. gpu = thr_info[i].cgpu->device_id;
  704. if (gpu == id) {
  705. thr = &thr_info[i];
  706. if (thr->cgpu->status != LIFE_WELL) {
  707. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  708. return;
  709. }
  710. gpus[id].enabled = true;
  711. tq_push(thr->q, &ping);
  712. }
  713. }
  714. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  715. }
  716. static void gpudisable(SOCKETTYPE c, char *param, bool isjson)
  717. {
  718. int id;
  719. if (nDevs == 0) {
  720. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  721. return;
  722. }
  723. if (param == NULL || *param == '\0') {
  724. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  725. return;
  726. }
  727. id = atoi(param);
  728. if (id < 0 || id >= nDevs) {
  729. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  730. return;
  731. }
  732. if (!gpus[id].enabled) {
  733. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  734. return;
  735. }
  736. gpus[id].enabled = false;
  737. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  738. }
  739. static void gpurestart(SOCKETTYPE c, char *param, bool isjson)
  740. {
  741. int id;
  742. if (nDevs == 0) {
  743. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  744. return;
  745. }
  746. if (param == NULL || *param == '\0') {
  747. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  748. return;
  749. }
  750. id = atoi(param);
  751. if (id < 0 || id >= nDevs) {
  752. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  753. return;
  754. }
  755. reinit_device(&gpus[id]);
  756. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  757. }
  758. static void gpucount(SOCKETTYPE c, char *param, bool isjson)
  759. {
  760. char buf[BUFSIZ];
  761. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  762. if (isjson)
  763. sprintf(buf, "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE, nDevs);
  764. else
  765. sprintf(buf, _GPUS ",Count=%d%c", nDevs, SEPARATOR);
  766. strcat(io_buffer, buf);
  767. }
  768. static void cpucount(SOCKETTYPE c, char *param, bool isjson)
  769. {
  770. char buf[BUFSIZ];
  771. int count = 0;
  772. #ifdef WANT_CPUMINE
  773. count = opt_n_threads > 0 ? num_processors : 0;
  774. #endif
  775. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  776. if (isjson)
  777. sprintf(buf, "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE, count);
  778. else
  779. sprintf(buf, _CPUS ",Count=%d%c", count, SEPARATOR);
  780. strcat(io_buffer, buf);
  781. }
  782. static void switchpool(SOCKETTYPE c, char *param, bool isjson)
  783. {
  784. struct pool *pool;
  785. int id;
  786. if (total_pools == 0) {
  787. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  788. return;
  789. }
  790. if (param == NULL || *param == '\0') {
  791. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  792. return;
  793. }
  794. id = atoi(param);
  795. if (id < 0 || id >= total_pools) {
  796. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  797. return;
  798. }
  799. pool = pools[id];
  800. pool->enabled = true;
  801. switch_pools(pool);
  802. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  803. }
  804. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  805. {
  806. int id;
  807. char *gpusep;
  808. if (nDevs == 0) {
  809. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  810. return false;
  811. }
  812. if (param == NULL || *param == '\0') {
  813. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  814. return false;
  815. }
  816. gpusep = strchr(param, GPUSEP);
  817. if (gpusep == NULL) {
  818. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  819. return false;
  820. }
  821. *(gpusep++) = '\0';
  822. id = atoi(param);
  823. if (id < 0 || id >= nDevs) {
  824. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  825. return false;
  826. }
  827. *gpu = id;
  828. *value = gpusep;
  829. return true;
  830. }
  831. static void gpuintensity(SOCKETTYPE c, char *param, bool isjson)
  832. {
  833. int id;
  834. char *value;
  835. int intensity;
  836. char intensitystr[7];
  837. if (!splitgpuvalue(param, &id, &value, isjson))
  838. return;
  839. if (!strncasecmp(value, DYNAMIC, 1)) {
  840. gpus[id].dynamic = true;
  841. strcpy(intensitystr, DYNAMIC);
  842. }
  843. else {
  844. intensity = atoi(value);
  845. if (intensity < -10 || intensity > 10) {
  846. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  847. return;
  848. }
  849. gpus[id].dynamic = false;
  850. gpus[id].intensity = intensity;
  851. sprintf(intensitystr, "%d", intensity);
  852. }
  853. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  854. }
  855. static void gpumem(SOCKETTYPE c, char *param, bool isjson)
  856. {
  857. #ifdef HAVE_ADL
  858. int id;
  859. char *value;
  860. int clock;
  861. if (!splitgpuvalue(param, &id, &value, isjson))
  862. return;
  863. clock = atoi(value);
  864. if (set_memoryclock(id, clock))
  865. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  866. else
  867. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  868. #else
  869. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  870. #endif
  871. }
  872. static void gpuengine(SOCKETTYPE c, char *param, bool isjson)
  873. {
  874. #ifdef HAVE_ADL
  875. int id;
  876. char *value;
  877. int clock;
  878. if (!splitgpuvalue(param, &id, &value, isjson))
  879. return;
  880. clock = atoi(value);
  881. if (set_engineclock(id, clock))
  882. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  883. else
  884. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  885. #else
  886. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  887. #endif
  888. }
  889. static void gpufan(SOCKETTYPE c, char *param, bool isjson)
  890. {
  891. #ifdef HAVE_ADL
  892. int id;
  893. char *value;
  894. int fan;
  895. if (!splitgpuvalue(param, &id, &value, isjson))
  896. return;
  897. fan = atoi(value);
  898. if (set_fanspeed(id, fan))
  899. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  900. else
  901. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  902. #else
  903. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  904. #endif
  905. }
  906. static void gpuvddc(SOCKETTYPE c, char *param, bool isjson)
  907. {
  908. #ifdef HAVE_ADL
  909. int id;
  910. char *value;
  911. float vddc;
  912. if (!splitgpuvalue(param, &id, &value, isjson))
  913. return;
  914. vddc = atof(value);
  915. if (set_vddc(id, vddc))
  916. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  917. else
  918. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  919. #else
  920. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  921. #endif
  922. }
  923. static void send_result(SOCKETTYPE c, bool isjson);
  924. void doquit(SOCKETTYPE c, char *param, bool isjson)
  925. {
  926. if (isjson)
  927. strcpy(io_buffer, JSON_START JSON_BYE);
  928. else
  929. strcpy(io_buffer, _BYE);
  930. send_result(c, isjson);
  931. *io_buffer = '\0';
  932. bye = 1;
  933. kill_work();
  934. }
  935. void dosave(SOCKETTYPE c, char *param, bool isjson)
  936. {
  937. FILE *fcfg;
  938. if (param == NULL || *param == '\0') {
  939. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  940. return;
  941. }
  942. fcfg = fopen(param, "w");
  943. if (!fcfg) {
  944. strcpy(io_buffer, message(MSG_BADFN, 0, param, isjson));
  945. return;
  946. }
  947. write_config(fcfg);
  948. fclose(fcfg);
  949. strcpy(io_buffer, message(MSG_SAVED, 0, param, isjson));
  950. }
  951. struct CMDS {
  952. char *name;
  953. void (*func)(SOCKETTYPE, char *, bool);
  954. } cmds[] = {
  955. { "version", apiversion },
  956. { "config", minerconfig },
  957. { "devs", devstatus },
  958. { "pools", poolstatus },
  959. { "summary", summary },
  960. { "gpuenable", gpuenable },
  961. { "gpudisable", gpudisable },
  962. { "gpurestart", gpurestart },
  963. { "gpu", gpudev },
  964. #ifdef WANT_CPUMINE
  965. { "cpu", cpudev },
  966. #endif
  967. { "gpucount", gpucount },
  968. { "cpucount", cpucount },
  969. { "switchpool", switchpool },
  970. { "gpuintensity", gpuintensity },
  971. { "gpumem", gpumem},
  972. { "gpuengine", gpuengine},
  973. { "gpufan", gpufan},
  974. { "gpuvddc", gpuvddc},
  975. { "save", dosave },
  976. { "quit", doquit },
  977. { NULL }
  978. };
  979. static void send_result(SOCKETTYPE c, bool isjson)
  980. {
  981. int n;
  982. int len;
  983. if (isjson)
  984. strcat(io_buffer, JSON_END);
  985. len = strlen(io_buffer);
  986. if (opt_debug)
  987. applog(LOG_DEBUG, "DBG: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : "");
  988. // ignore failure - it's closed immediately anyway
  989. n = send(c, io_buffer, len+1, 0);
  990. if (opt_debug) {
  991. if (SOCKETFAIL(n))
  992. applog(LOG_DEBUG, "DBG: send failed: %s", SOCKERRMSG);
  993. else
  994. applog(LOG_DEBUG, "DBG: sent %d", n);
  995. }
  996. }
  997. static void tidyup()
  998. {
  999. bye = 1;
  1000. if (sock != INVSOCK) {
  1001. shutdown(sock, SHUT_RDWR);
  1002. CLOSESOCKET(sock);
  1003. sock = INVSOCK;
  1004. }
  1005. if (msg_buffer != NULL) {
  1006. free(msg_buffer);
  1007. msg_buffer = NULL;
  1008. }
  1009. if (io_buffer != NULL) {
  1010. free(io_buffer);
  1011. io_buffer = NULL;
  1012. }
  1013. }
  1014. void api(void)
  1015. {
  1016. char buf[BUFSIZ];
  1017. char param_buf[BUFSIZ];
  1018. const char *localaddr = "127.0.0.1";
  1019. SOCKETTYPE c;
  1020. int n, bound;
  1021. char *connectaddr;
  1022. char *binderror;
  1023. time_t bindstart;
  1024. short int port = opt_api_port;
  1025. struct sockaddr_in serv;
  1026. struct sockaddr_in cli;
  1027. socklen_t clisiz;
  1028. char *cmd;
  1029. char *param;
  1030. bool addrok;
  1031. json_error_t json_err;
  1032. json_t *json_config;
  1033. json_t *json_val;
  1034. bool isjson;
  1035. bool did;
  1036. int i;
  1037. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1038. sleep(opt_log_interval);
  1039. if (!opt_api_listen) {
  1040. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1041. return;
  1042. }
  1043. sock = socket(AF_INET, SOCK_STREAM, 0);
  1044. if (sock == INVSOCK) {
  1045. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1046. return;
  1047. }
  1048. memset(&serv, 0, sizeof(serv));
  1049. serv.sin_family = AF_INET;
  1050. if (!opt_api_network) {
  1051. serv.sin_addr.s_addr = inet_addr(localaddr);
  1052. if (serv.sin_addr.s_addr == INVINETADDR) {
  1053. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1054. return;
  1055. }
  1056. }
  1057. serv.sin_port = htons(port);
  1058. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1059. bound = 0;
  1060. bindstart = time(NULL);
  1061. while (bound == 0) {
  1062. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1063. binderror = SOCKERRMSG;
  1064. if ((time(NULL) - bindstart) > 61)
  1065. break;
  1066. else {
  1067. applog(LOG_WARNING, "API bind to port %d failed - trying again in 15sec", port);
  1068. sleep(15);
  1069. }
  1070. }
  1071. else
  1072. bound = 1;
  1073. }
  1074. if (bound == 0) {
  1075. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1076. return;
  1077. }
  1078. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1079. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1080. CLOSESOCKET(sock);
  1081. return;
  1082. }
  1083. if (opt_api_network)
  1084. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1085. else
  1086. applog(LOG_WARNING, "API running in restricted access mode");
  1087. io_buffer = malloc(MYBUFSIZ+1);
  1088. msg_buffer = malloc(MYBUFSIZ+1);
  1089. while (bye == 0) {
  1090. clisiz = sizeof(cli);
  1091. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1092. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1093. goto die;
  1094. }
  1095. if (opt_api_network)
  1096. addrok = true;
  1097. else {
  1098. connectaddr = inet_ntoa(cli.sin_addr);
  1099. addrok = (strcmp(connectaddr, localaddr) == 0);
  1100. }
  1101. if (opt_debug) {
  1102. connectaddr = inet_ntoa(cli.sin_addr);
  1103. applog(LOG_DEBUG, "DBG: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1104. }
  1105. if (addrok) {
  1106. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1107. if (SOCKETFAIL(n))
  1108. buf[0] = '\0';
  1109. else
  1110. buf[n] = '\0';
  1111. if (opt_debug) {
  1112. if (SOCKETFAIL(n))
  1113. applog(LOG_DEBUG, "DBG: recv failed: %s", SOCKERRMSG);
  1114. else
  1115. applog(LOG_DEBUG, "DBG: recv command: (%d) '%s'", n, buf);
  1116. }
  1117. if (!SOCKETFAIL(n)) {
  1118. did = false;
  1119. if (*buf != ISJSON) {
  1120. isjson = false;
  1121. param = strchr(buf, SEPARATOR);
  1122. if (param != NULL)
  1123. *(param++) = '\0';
  1124. cmd = buf;
  1125. }
  1126. else {
  1127. isjson = true;
  1128. param = NULL;
  1129. json_config = json_loadb(buf, n, 0, &json_err);
  1130. if (!json_is_object(json_config)) {
  1131. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1132. send_result(c, isjson);
  1133. did = true;
  1134. }
  1135. else {
  1136. json_val = json_object_get(json_config, JSON_COMMAND);
  1137. if (json_val == NULL) {
  1138. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1139. send_result(c, isjson);
  1140. did = true;
  1141. }
  1142. else {
  1143. if (!json_is_string(json_val)) {
  1144. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1145. send_result(c, isjson);
  1146. did = true;
  1147. }
  1148. else {
  1149. cmd = (char *)json_string_value(json_val);
  1150. json_val = json_object_get(json_config, JSON_PARAMETER);
  1151. if (json_is_string(json_val))
  1152. param = (char *)json_string_value(json_val);
  1153. else if (json_is_integer(json_val)) {
  1154. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1155. param = param_buf;
  1156. } else if (json_is_real(json_val)) {
  1157. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1158. param = param_buf;
  1159. }
  1160. }
  1161. }
  1162. }
  1163. }
  1164. if (!did)
  1165. for (i = 0; cmds[i].name != NULL; i++) {
  1166. if (strcmp(cmd, cmds[i].name) == 0) {
  1167. (cmds[i].func)(c, param, isjson);
  1168. send_result(c, isjson);
  1169. did = true;
  1170. break;
  1171. }
  1172. }
  1173. if (!did) {
  1174. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1175. send_result(c, isjson);
  1176. }
  1177. }
  1178. }
  1179. CLOSESOCKET(c);
  1180. }
  1181. die:
  1182. tidyup();
  1183. }