├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── config.yaml ├── flashair.lua ├── sample.lua └── spec └── module_spec.lua /.gitignore: -------------------------------------------------------------------------------- 1 | temp.lua 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | 3 | sudo: false 4 | 5 | env: 6 | matrix: 7 | #- LUA="lua 5.1" SOCKET=true 8 | #- LUA="lua 5.1" 9 | - LUA="lua 5.2" SOCKET=true 10 | #- LUA="lua 5.2" 11 | #- LUA="lua 5.3" SOCKET=true 12 | #- LUA="lua 5.3" 13 | #- LUA="luajit 2.0" 14 | #- LUA="luajit 2.1" SOCKET=true SYSCALL=true 15 | #- LUA="luajit 2.1" SYSCALL=true 16 | #- LUA="luajit 2.1" 17 | #- LUA="luajit @" 18 | 19 | before_install: 20 | - pip install hererocks 21 | - hererocks here -r^ --$LUA # Install latest LuaRocks version 22 | # plus the Lua version for this build job 23 | # into 'here' subdirectory 24 | - export PATH=$PATH:$PWD/here/bin # Add directory with all installed binaries to PATH 25 | - eval `luarocks path --bin` 26 | - luarocks install luacov-coveralls 27 | - luarocks install busted 28 | - luarocks install luafilesystem 29 | - luarocks install luasocket 30 | - luarocks install luaossl 31 | - luarocks install lyaml YAML_LIBDIR=/usr/lib/x86_64-linux-gnu 32 | 33 | install: 34 | #- luarocks make 35 | - if [ "$SOCKET" = "true" ]; then luarocks install luasocket; fi 36 | - if [ "$SYSCALL" = "true" ]; then luarocks install ljsyscall; fi 37 | 38 | script: 39 | - busted -c spec 40 | 41 | after_success: 42 | - luacov-coveralls -v 43 | 44 | notifications: 45 | email: 46 | on_success: change 47 | on_failure: always 48 | slack: 49 | secure: T4b0kjRp3Qa0DuXUsMGxxN1g3rNFhzwFdPZb0r8nhSPaIg/2s26vrD5mD8TzEdNW6lBCwy48ruvMEfB1cv7Wcz7LC8O6UGiLF3wEUALd1G+oBDBKWNRSIbnLnANTJDgXsZE+aQK3eb9fik/T/XFc/V2btq0ApOwZO/2L/sZFsdY= 50 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Yoshiki Sato 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # flashair-lua-dev 2 | 3 | FlashAir独自のLua関数を利用するためのライブラリ。 4 | このライブラリが提供するオブジェクト fa が FlashAir の fa の様に振る舞うため、FlashAir外でも開発が可能になる。 5 | 6 | * master: [![Build Status](https://travis-ci.org/xight/flashair-lua-dev.svg?branch=master)](https://travis-ci.org/xight/flashair-lua-dev) 7 | * develop: [![Build Status](https://travis-ci.org/xight/flashair-lua-dev.svg?branch=develop)](https://travis-ci.org/xight/flashair-lua-dev) 8 | 9 | # Install 10 | 11 | ## Install lua library 12 | 13 | % luarocks install luasocket 14 | % luarocks install luaossl 15 | % luarocks install lyaml 16 | % luarocks install busted 17 | 18 | ## git clone & testing 19 | 20 | % git clone https://github.com/xight/flashair-lua-dev 21 | % cd flashair-lua-dev 22 | % busted spec 23 | 24 | ## Run sample script 25 | 26 | % lua sample.lua 27 | 28 | # Usage 29 | 30 | ```lua 31 | require("flashair") 32 | 33 | local b, c, h = fa.request("http://example.com/") 34 | print(b) 35 | 36 | local b, c, h = fa.request{url = "http://example.com/"} 37 | print(b) 38 | ``` 39 | 40 | # Requirement 41 | 42 | * [luasocket](https://github.com/diegonehab/luasocket) 43 | * ~~[luacrypto](https://github.com/mkottman/luacrypto)~~ (deprecated) 44 | * [luaossl](http://25thandclement.com/~william/projects/luaossl.html) 45 | * [lyaml](https://github.com/gvvaughan/lyaml) 46 | * [busted](https://github.com/Olivine-Labs/busted) for unit testing 47 | 48 | # ToDo 49 | 50 | * fa.pio の仕様確認と実装 51 | * fa.ReadStatusReg()の仕様確認と実装 52 | * fa.FTP の実装 53 | 54 | # Reference 55 | 56 | * [FlashAir Developers - APIガイド - Lua機能](https://flashair-developers.github.io/website/docs/api/lua.html) 57 | * [SlideShare - xightorg - 2015-03-21 FlashAir 進捗報告会](http://www.slideshare.net/xightorg/2015-0321-flashair) 58 | 59 | # License 60 | 61 | Copyright (c) 2017 Yoshiki Sato 62 | This software is released under the MIT license. 63 | https://github.com/xight/flashair-lua-dev/blob/master/LICENSE 64 | -------------------------------------------------------------------------------- /config.yaml: -------------------------------------------------------------------------------- 1 | ssid: "SSID" 2 | mac_address: "12:34:56:AB:CD:EF" 3 | ip_address: "192.168.2.250" 4 | subnet_mask: "255.255.255.0" 5 | default_gateway: "192.168.2.1" 6 | preferred_dns: "192.168.2.1" 7 | alternate_dns: "0.0.0.0" 8 | -------------------------------------------------------------------------------- /flashair.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | flashair-lua-dev 3 | http://github.com/xight/flashair-lua-dev 4 | ]] 5 | 6 | FlashAir = {} 7 | FlashAir.new = function() 8 | local socket = require("socket") 9 | local obj = {} 10 | local base = _G 11 | 12 | local lyaml = require("lyaml") 13 | local fh, msg = io.open("config.yaml","r") 14 | 15 | obj._network = {} 16 | if fh then 17 | local data = fh:read("*a") 18 | obj.config = lyaml.load(data) 19 | 20 | if obj.config.ip_address ~= nil then 21 | obj._network.ip_address = obj.config.ip_address 22 | end 23 | if obj.config.subnet_mask ~= nil then 24 | obj._network.subnet_mask = obj.config.subnet_mask 25 | end 26 | if obj.config.default_gateway ~= nil then 27 | obj._network.default_gateway = obj.config.default_gateway 28 | end 29 | end 30 | 31 | -- b, c, h = fa.request(url [, method [, headers [, file [, body [, bufsize [, redirect]]]]]]) 32 | local srequest = function(url, ...) 33 | local param = ... 34 | local method = nil 35 | local headers = nil 36 | local file = nil 37 | local reqbody = nil 38 | local bufsize = nil 39 | local redirect = nil 40 | if param ~= nil then 41 | method = param[1] 42 | headers = param[2] 43 | file = param[3] 44 | reqbody = param[4] 45 | bufsize = param[5] 46 | redirect = param[6] 47 | end 48 | 49 | local http = require("socket.http") 50 | local ltn12 = require("ltn12") 51 | 52 | local body = {} 53 | local b, c, h = http.request { 54 | url = url, 55 | sink = ltn12.sink.table(body), 56 | method = method, 57 | headers = headers, 58 | source = ltn12.source.string(reqbody), 59 | step = nil, 60 | proxy = nil, 61 | redirect = redirect, 62 | create = nil, 63 | } 64 | return table.concat(body), c, h 65 | end 66 | 67 | local trequest = function(...) 68 | local param = ... 69 | local url = param["url"] 70 | local method = param["method"] 71 | local headers = param["headers"] 72 | local file = param["file"] 73 | local reqbody = param["body"] 74 | local bufsize = param["bufsize"] 75 | local redirect = param["redirect"] 76 | 77 | local http = require("socket.http") 78 | local ltn12 = require("ltn12") 79 | 80 | local body = {} 81 | local b, c, h = http.request { 82 | url = url, 83 | sink = ltn12.sink.table(body), 84 | method = method, 85 | headers = headers, 86 | source = ltn12.source.string(reqbody), 87 | step = nil, 88 | proxy = nil, 89 | redirect = redirect, 90 | create = nil, 91 | } 92 | return table.concat(body), c, h 93 | end 94 | 95 | obj.request = socket.protect(function(reqt, body) 96 | if base.type(reqt) == "string" then return srequest(reqt, body) 97 | else return trequest(reqt) end 98 | end) 99 | 100 | obj.HTTPGetFile = function(uri, filepath, ...) 101 | local param = {...} 102 | 103 | local socket_url = require("socket.url") 104 | local parsed_url = socket_url.parse(uri) 105 | local user = param[1] 106 | local pass = param[2] 107 | 108 | if (user ~= nil and pass ~= nil) then 109 | local url = socket_url.parse(uri) 110 | local parsed_url = socket_url.parse(uri) 111 | parsed_url.user = user 112 | parsed_url.password = pass 113 | uri = socket_url.build(parsed_url) 114 | end 115 | 116 | local http = require("socket.http") 117 | local ltn12 = require("ltn12") 118 | 119 | -- get HTTP status code 120 | local b, c, h = http.request { 121 | url = uri, 122 | } 123 | 124 | local ret = nil 125 | if c == 200 then 126 | ret = 1 127 | 128 | local b, c, h = http.request { 129 | url = uri, 130 | sink = ltn12.sink.file(io.open(filepath,"w")), 131 | } 132 | end 133 | 134 | return ret 135 | end 136 | 137 | obj.pio = function(ctrl, data) 138 | -- ad hoc 139 | local s = 1 140 | local indata = 1 141 | return s, indata 142 | end 143 | 144 | -- not implement 145 | obj.FTP = function(cmd, uri, filename) 146 | end 147 | 148 | -- md5 = fa.md5(str) 149 | -- obsolete (< 3.0.0) 150 | obj.md5 = function(str) 151 | return obj.hash("md5", str) 152 | end 153 | 154 | -- hash = fa.hash(name, data, key) 155 | obj.hash = function(name, data, key) 156 | local function tohex(s) 157 | return (string.gsub(s, ".", function (c) 158 | return string.format("%.2x", string.byte(c)) 159 | end)) 160 | end -- tohex 161 | 162 | ret = nil 163 | 164 | if (name == "md5") then 165 | ret = tohex(require"openssl.digest".new("md5"):final(data)) 166 | elseif (name == "sha1") then 167 | ret = tohex(require"openssl.digest".new("sha1"):final(data)) 168 | elseif (name == "sha256") then 169 | ret = tohex(require"openssl.digest".new("sha256"):final(data)) 170 | elseif (name == "hmac-sha256") then 171 | ret = tohex(require"openssl.hmac".new("secret","sha256"):final(data)) 172 | end 173 | return ret 174 | end 175 | 176 | -- obsolete (using LuaCrypto) 177 | obj._hash = function(name, data, key) 178 | local crypto = require("crypto") 179 | ret = nil 180 | if (name == "md5") then 181 | ret = crypto.digest("md5", data) 182 | elseif (name == "sha1") then 183 | ret = crypto.digest("sha1",data) 184 | elseif (name == "sha256") then 185 | ret = crypto.digest("sha256",data) 186 | elseif (name == "hmac-sha256") then 187 | ret = crypto.hmac.digest("sha256",data, key) 188 | end 189 | return ret 190 | end 191 | 192 | 193 | -- count = fa.Scan([ssid]) 194 | obj.Scan = function(...) 195 | local count = 1 196 | return count 197 | end 198 | 199 | -- ssid, other = fa.GetScanInfo(num) 200 | obj.GetScanInfo = function(num) 201 | local ssid = obj.config.ssid 202 | local other = {} 203 | return ssid, other 204 | end 205 | 206 | obj.Connect = function(ssid, networkkey) 207 | io.stderr:write("Connect: " .. ssid .. ", " .. networkkey .. "\n") 208 | end 209 | 210 | obj.Establish = function(ssid, networkkey, encmode) 211 | io.stderr:write("Establish: " .. ssid .. ", " .. networkkey .. ", " ..encmode .. "\n") 212 | end 213 | 214 | obj.Bridge = function(ssid, networkkey, encmode, brgssid, brgnetworkkey) 215 | io.stderr:write("Bridge: " .. ssid .. ", " .. networkkey .. ", " ..encmode) 216 | io.stderr:write(brgssid .. ", " .. brgnetworkkey .. "\n") 217 | end 218 | 219 | obj.Disconnect = function() 220 | io.stderr:write("Disconnect\n") 221 | end 222 | 223 | obj.sleep = function(msec) 224 | local sec = msec / 1000 225 | os.execute("sleep ".. sec) 226 | end 227 | 228 | -- fa.sharedmemory(command, addr, len, wdata) 229 | obj._sharedmemory = {} 230 | 231 | obj.sharedmemory = function(command, addr, len, wdata) 232 | assert(command == "read" or command == "write", "command must be a \"write\" or \"read\"") 233 | assert(type(addr) == "number", "addr must be a number") 234 | assert(type(len) == "number", "len must be a number") 235 | 236 | ret = nil 237 | 238 | 239 | if command == "write" then 240 | wdata_t = {} 241 | wdata:gsub(".",function(c) table.insert(wdata_t,c) end) 242 | 243 | j = 1 244 | for i = addr + 1, addr + len do 245 | -- print(i, j, wdata_t[j]) 246 | obj._sharedmemory[i] = wdata_t[j] 247 | j = j + 1 248 | end 249 | ret = 1 250 | 251 | elseif command == "read" then 252 | ret = "" 253 | for i = addr + 1, addr + len do 254 | -- print(i, obj._sharedmemory[i]) 255 | if obj._sharedmemory[i] == nil then 256 | obj._sharedmemory[i] = " " 257 | end 258 | ret = ret .. obj._sharedmemory[i] 259 | end 260 | end 261 | return ret 262 | end 263 | 264 | -- fa.SetCert(filename) 265 | -- not implement 266 | obj.SetCert = function(filename) 267 | return 1 268 | end 269 | 270 | -- fa.strconvert(format, orgstr) 271 | -- not implement 272 | obj.strconvert = function(format, orgstr) 273 | assert(format == "sjis2utf8" or format == "utf82sjis", "format must be a \"sjis2utf8\" or \"utf82sjis\"") 274 | return nil 275 | end 276 | 277 | -- fa.SetChannel(channelNo) 278 | -- not implement 279 | obj.SetChannel = function(channelNo) 280 | assert(type(channelNo) == "number", "channelNo must be a number") 281 | end 282 | 283 | -- fa.MailSend(from,headers,body,server,user,password, attachment, ContentType) 284 | -- not implement 285 | obj.MailSend= function(from,headers,body,server,user,password, attachment, ContentType) 286 | local success_message = "MailSend is success." 287 | local failure_message = "Error: It filed to send." 288 | return success_message 289 | end 290 | 291 | -- fa.spi(command, data) 292 | -- not implement 293 | obj.spi = function(command, data) 294 | assert(command == "init" or command == "mode" or command == "bit" or command == "write" or command == "read" or command == "cs", "command must be a \"init\", \"mode\", \"bit\", \"write\", \"read\" or \"cs\"") 295 | return 1 296 | end 297 | 298 | obj.ReadStatusReg = function() 299 | --[[ 300 | ssid -- 17-80 (535349440000...00) 301 | mac_address -- 97-108 (123456ABCDEF) 302 | ip_address -- 161-168 (c0a802fa) 303 | subnet_mask -- 169-176 (ffffffff) 304 | default_gateway -- 177-184 (c0a80201) 305 | preferred_dns -- 185-192 (c0a80201) 306 | alternate_dns -- 193-200 (00000000) 307 | ]] 308 | -- split 309 | local split_it = function(str, sep) 310 | if str == nil then return nil end 311 | assert(type(str) == "string", "str must be a string") 312 | assert(type(sep) == "string", "sep must be a string") 313 | return string.gmatch(str, "[^\\" .. sep .. "]+") 314 | end 315 | local split = function(str, sep) 316 | local ret = {} 317 | for seg in split_it(str, sep) do 318 | ret[#ret+1] = seg 319 | end 320 | return ret 321 | end 322 | 323 | -- hex generate 324 | local ip2hex = function(ip) 325 | local ip_t = split(ip,".") 326 | local ip_hex = "" 327 | for i,v in ipairs(ip_t) do 328 | ip_hex = ip_hex .. string.format("%02x",v) 329 | end 330 | return ip_hex 331 | end 332 | 333 | local mac2hex = function(mac) 334 | local mac_t = split(mac,":") 335 | return table.concat(mac_t,"") 336 | end 337 | 338 | local ssid2hex = function(ssid) 339 | local ssid_hex = "" 340 | for i = 1, string.len(ssid) do 341 | ssid_hex = ssid_hex .. string.format("%02x",string.byte(ssid,i)) 342 | end 343 | -- zero padding (size 64) 344 | for i = 1, 64 - string.len(ssid_hex) do 345 | ssid_hex = ssid_hex .. "0" 346 | end 347 | return ssid_hex 348 | end 349 | 350 | -- 1-------------16 351 | local ret = "000000000000a000" 352 | -- 17-80 (64byte) 353 | ret = ret .. ssid2hex(obj.config.ssid) 354 | -- 81------------96 355 | ret = ret .. "06640b0000000000" 356 | -- 97-108 (12byte) 357 | ret = ret .. mac2hex(obj.config.mac_address) 358 | -- 109----------------------------------------------160 359 | ret = ret .. "0000000000000000000000000000000000000000000000000000" 360 | -- 161-200 361 | ret = ret .. ip2hex(obj.config.ip_address) 362 | ret = ret .. ip2hex(obj.config.subnet_mask) 363 | ret = ret .. ip2hex(obj.config.default_gateway) 364 | ret = ret .. ip2hex(obj.config.preferred_dns) 365 | ret = ret .. ip2hex(obj.config.alternate_dns) 366 | -- 201----------------------------------240 367 | ret = ret .. "0000000000000000000000000000000000000000" 368 | return ret 369 | end 370 | 371 | -- ip, mask, gw = fa.ip(ipaddress, subnetmask, gateway) 372 | obj.ip = function(ipaddress, subnetmask, gateway) 373 | if ipaddress ~= nil then 374 | obj._network.ip_address = ipaddress 375 | end 376 | if subnetmask ~= nil then 377 | obj._network.subnet_mask = subnetmask 378 | end 379 | 380 | if gateway ~= nil then 381 | obj._network.default_gateway = gateway 382 | end 383 | 384 | return obj._network.ip_address, obj._network.subnet_mask, obj._network.default_gateway 385 | end 386 | 387 | -- result = fa.WlanLink() 388 | -- not implement 389 | obj.WlanLink = function() 390 | local connected = 1 391 | local disconnect = 0 392 | return connected 393 | end 394 | 395 | -- fa.remove(filename) 396 | -- not implement 397 | obj.remove = function(filename) 398 | end 399 | 400 | -- fa.rename(oldfile, newfile) 401 | -- not implement 402 | obj.rename= function(oldfile, newfile) 403 | end 404 | 405 | -- res, data1, data2, data3, ... = fa.i2c(table) 406 | -- not implement 407 | obj.i2c = function(table) 408 | end 409 | 410 | -- result, filelist, time = fa.search(type, path, searchtime) 411 | -- not implement 412 | obj.search = function(type, path, searchtime) 413 | end 414 | 415 | -- result = fa.control("time"[, savetime]) 416 | -- result = fa.control("time"[, savetime]) 417 | -- result = fa.control("fioget") 418 | -- result = fa.control("fioset", enable) 419 | -- not implement 420 | obj.control= function(arg, val) 421 | end 422 | 423 | -- cnt,tbl = fa.ConnectedSTA() 424 | -- not implement 425 | obj.ConnectedSTA = function() 426 | end 427 | 428 | -- res, type, payload = fa.websocket(table) 429 | -- not implement 430 | obj.websocket = function(table) 431 | end 432 | 433 | return obj 434 | end 435 | 436 | fa = FlashAir.new() 437 | -------------------------------------------------------------------------------- /sample.lua: -------------------------------------------------------------------------------- 1 | require("flashair") 2 | 3 | local b, c, h = fa.request{url = "http://example.com/"} 4 | print(b) 5 | -------------------------------------------------------------------------------- /spec/module_spec.lua: -------------------------------------------------------------------------------- 1 | require("flashair") 2 | 3 | describe("flashair", function() 4 | local config = {} 5 | local lyaml 6 | local download_file1,download_file2 7 | 8 | setup(function() 9 | download_file1 = "__test_download1" 10 | download_file2 = "__test_download2" 11 | lyaml = require("lyaml") 12 | local fh, msg = io.open("config.yaml","r") 13 | 14 | if fh then 15 | local data = fh:read("*a") 16 | config = lyaml.load(data) 17 | end 18 | end) 19 | 20 | teardown(function() 21 | os.remove(download_file1) 22 | os.remove(download_file2) 23 | end) 24 | 25 | it("check request (srequest)", function() 26 | local b, c, h = fa.request("http://example.com/") 27 | assert.are.equals(c, 200) 28 | end) 29 | 30 | it("check request (srequest / not found)", function() 31 | local b, c, h = fa.request("http://example.com/not-exist") 32 | assert.are.equals(c, 404) 33 | end) 34 | 35 | it("check request (srequest / not exist domain)", function() 36 | local b, c, h = fa.request("http://not-exist.com/") 37 | assert.are.equals(c, 'host or service not provided, or not known') 38 | end) 39 | 40 | it("check request (trequest)", function() 41 | local b, c, h = fa.request{url = "http://example.com/"} 42 | assert.are.equals(c, 200) 43 | end) 44 | 45 | it("check request (trequest / not found)", function() 46 | local b, c, h = fa.request{url = "http://example.com/not-exist"} 47 | assert.are.equals(c, 404) 48 | end) 49 | 50 | it("check request (trequest / not exist domain)", function() 51 | local b, c, h = fa.request{url = "http://not-exist.com/"} 52 | assert.are.equals(c, 'host or service not provided, or not known') 53 | end) 54 | 55 | it("check HTTPGetFile", function() 56 | local state = fa.HTTPGetFile("http://example.com/",download_file1) 57 | assert.are.equals(state, 1) 58 | end) 59 | 60 | it("check HTTPGetFile (not exist file)", function() 61 | local state = fa.HTTPGetFile("http://example.com/not-exist",download_file2) 62 | assert.are.equals(state, nil) 63 | end) 64 | 65 | it("check HTTPGetFile (not exist domain)", function() 66 | local state = fa.HTTPGetFile("http://not-exist.com/",download_file2) 67 | assert.are.equals(state, nil) 68 | end) 69 | 70 | it("check pio", function() 71 | local ctrl = 1 72 | local data = 1 73 | fa.pio(ctrl, data) 74 | assert.are.equals(1,1) 75 | end) 76 | 77 | -- not implement 78 | it("check FTP", function() 79 | end) 80 | 81 | -- obsolete (< 3.0.0) 82 | it("check md5", function() 83 | local md5_a = fa.md5("a") 84 | local md5_KNOWN_a = "0cc175b9c0f1b6a831c399e269772661" 85 | assert.are.equals(md5_a, md5_KNOWN_a) 86 | end) 87 | 88 | it("check hash (md5)", function() 89 | local md5_a = fa.hash("md5","flashair") 90 | local md5_KNOWN_a = "ee626b402376f878d0de4a7a81df7675" 91 | assert.are.equals(md5_a, md5_KNOWN_a) 92 | end) 93 | 94 | it("check hash (sha1)", function() 95 | local md5_a = fa.hash("sha1","flashair") 96 | local md5_KNOWN_a = "c7b0cbd0e25e56f5e15e3aba767e816ff025b26c" 97 | assert.are.equals(md5_a, md5_KNOWN_a) 98 | end) 99 | 100 | it("check hash (sha256)", function() 101 | local md5_a = fa.hash("sha256","flashair") 102 | local md5_KNOWN_a = "6594b08c58521f2ff8dd9f608fe355b5e24067b38556216486a13939070ffb86" 103 | assert.are.equals(md5_a, md5_KNOWN_a) 104 | end) 105 | 106 | it("check hash (hmac-sha256)", function() 107 | local md5_a = fa.hash("hmac-sha256","flashair","secret") 108 | local md5_KNOWN_a = "0d201feddd40baa509230859e1b85ed173170d099c2e6c3babad2b6efc6aa400" 109 | assert.are.equals(md5_a, md5_KNOWN_a) 110 | end) 111 | 112 | -- not implement 113 | it("check Scan", function() 114 | end) 115 | 116 | it("check GetScanInfo", function() 117 | local ssid = config.ssid 118 | local ret_ssid, ret_other = fa.GetScanInfo(0) 119 | assert.are.equals(ssid,ret_ssid) 120 | end) 121 | 122 | -- not implement 123 | it("check Connect", function() 124 | end) 125 | 126 | -- not implement 127 | it("check Establish", function() 128 | end) 129 | 130 | -- not implement 131 | it("check Bridge", function() 132 | end) 133 | 134 | -- not implement 135 | it("check Disconnect", function() 136 | end) 137 | 138 | it("check sleep", function() 139 | local sleep_sec = 1 140 | local t = os.time() 141 | fa.sleep(sleep_sec * 1000) 142 | local t2 = os.time() 143 | assert.are.equals(t2 - t, sleep_sec) 144 | end) 145 | 146 | -- fa.sharedmemory(command, addr, len, wdata) 147 | -- https://flashair-developers.com/ja/documents/api/lua/reference/#sharedmemory 148 | it("check sharedmemory (write)", function() 149 | local ret = fa.sharedmemory("write", 0, 8, "flashair") 150 | assert.are.equals(ret, 1) 151 | end) 152 | 153 | it("check sharedmemory (read)", function() 154 | local ret = fa.sharedmemory("read", 0, 8, 0) 155 | assert.are.equals(ret, "flashair") 156 | end) 157 | 158 | it("check sharedmemory (write 2)", function() 159 | local ret = fa.sharedmemory("write", 1, 8, "flashair") 160 | assert.are.equals(ret, 1) 161 | end) 162 | 163 | it("check sharedmemory (read 2)", function() 164 | local ret = fa.sharedmemory("read", 0, 9, 0) 165 | assert.are.equals(ret, "fflashair") 166 | end) 167 | 168 | --[[ 169 | it("check sharedmemory (write fail : addr < 0)", function() 170 | local ret = fa.sharedmemory("write", -1, 8, "flashair") 171 | assert.are.equals(ret, nil) 172 | end) 173 | 174 | it("check sharedmemory (write fail : addr > 511)", function() 175 | local ret = fa.sharedmemory("write", 0, 8, "flashair") 176 | assert.are.equals(ret, nil) 177 | end) 178 | 179 | it("check sharedmemory (write fail : len < length(wdata))", function() 180 | local ret = fa.sharedmemory("write", 0, 8, "flashair") 181 | assert.are.equals(ret, nil) 182 | end) 183 | ]] 184 | 185 | -- not implement 186 | it("check SetCert", function() 187 | end) 188 | 189 | -- not implement 190 | it("check strconvert", function() 191 | end) 192 | 193 | -- not implement 194 | it("check SetChannel", function() 195 | end) 196 | 197 | -- not implement 198 | it("check MailSend", function() 199 | end) 200 | 201 | -- not implement 202 | it("check spi", function() 203 | end) 204 | 205 | it("check ReadStatusReg return value length", function() 206 | local status_reg = fa.ReadStatusReg() 207 | assert.are.equals(string.len(status_reg), 240) 208 | end) 209 | 210 | it("check ReadStatusReg ip address", function() 211 | local ip = config.ip_address 212 | local ip_hex = string.sub(fa.ReadStatusReg(),161,168) 213 | ip1 = tonumber(string.sub(ip_hex,1,2),16) 214 | ip2 = tonumber(string.sub(ip_hex,3,4),16) 215 | ip3 = tonumber(string.sub(ip_hex,5,6),16) 216 | ip4 = tonumber(string.sub(ip_hex,7,8),16) 217 | local ret_ip = ip1 .. "." .. ip2 .. "." .. ip3 .. "." .. ip4 218 | assert.are.equals(ret_ip , ip) 219 | end) 220 | 221 | it("check ip (get)", function() 222 | local obj = {} 223 | local lyaml = require("lyaml") 224 | local fh, msg = io.open("config.yaml","r") 225 | if fh then 226 | local data = fh:read("*a") 227 | obj.config = lyaml.load(data) 228 | end 229 | 230 | local ip, mask, gw = fa.ip() 231 | assert.are.equals(ip, obj.config.ip_address) 232 | assert.are.equals(mask, obj.config.subnet_mask) 233 | assert.are.equals(gw, obj.config.default_gateway) 234 | end) 235 | 236 | it("check ip (set)", function() 237 | local test_ip = "192.168.11.2" 238 | local test_mask = "255.255.255.0" 239 | local test_gw = "192.168.11.1" 240 | fa.ip(test_ip, test_mask, test_gw) 241 | local ip, mask, gw = fa.ip() 242 | assert.are.equals(ip, test_ip) 243 | assert.are.equals(mask, test_mask) 244 | assert.are.equals(gw, test_gw) 245 | end) 246 | 247 | -- not implement 248 | it("check WlanLink", function() 249 | local connected = 1 250 | assert.are.equals(fa.WlanLink(), connected) 251 | end) 252 | 253 | -- not implement 254 | it("check i2c", function() 255 | end) 256 | 257 | end) 258 | --------------------------------------------------------------------------------