alpha server and client for st flash
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
.idea/workspace.xml
|
||||
.idea/uiDesigner.xml
|
||||
out
|
||||
Generated
+22
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<resourceExtensions />
|
||||
<wildcardResourcePatterns>
|
||||
<entry name="!?*.java" />
|
||||
<entry name="!?*.form" />
|
||||
<entry name="!?*.class" />
|
||||
<entry name="!?*.groovy" />
|
||||
<entry name="!?*.scala" />
|
||||
<entry name="!?*.flex" />
|
||||
<entry name="!?*.kt" />
|
||||
<entry name="!?*.clj" />
|
||||
<entry name="!?*.aj" />
|
||||
</wildcardResourcePatterns>
|
||||
<annotationProcessing>
|
||||
<profile default="true" name="Default" enabled="false">
|
||||
<processorPath useClasspath="true" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<component name="CopyrightManager">
|
||||
<settings default="" />
|
||||
</component>
|
||||
@@ -0,0 +1,12 @@
|
||||
<component name="libraryTable">
|
||||
<library name="KotlinJavaRuntime">
|
||||
<CLASSES>
|
||||
<root url="jar://$PROJECT_DIR$/lib/kotlin-runtime.jar!/" />
|
||||
<root url="jar://$PROJECT_DIR$/lib/kotlin-reflect.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES>
|
||||
<root url="jar://$PROJECT_DIR$/lib/kotlin-runtime-sources.jar!/" />
|
||||
</SOURCES>
|
||||
</library>
|
||||
</component>
|
||||
@@ -0,0 +1,11 @@
|
||||
<component name="libraryTable">
|
||||
<library name="netty-all-4.1.2.Final-sources">
|
||||
<CLASSES>
|
||||
<root url="jar://$PROJECT_DIR$/lib/netty-all-4.1.2.Final.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES>
|
||||
<root url="jar://$PROJECT_DIR$/lib/netty-all-4.1.2.Final-sources.jar!/" />
|
||||
</SOURCES>
|
||||
</library>
|
||||
</component>
|
||||
@@ -0,0 +1,9 @@
|
||||
<component name="libraryTable">
|
||||
<library name="protobuf-java-3.0.0-beta-3">
|
||||
<CLASSES>
|
||||
<root url="jar://$USER_HOME$/Downloads/protobuf-java-3.0.0-beta-3.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</component>
|
||||
Generated
+26
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="EntryPointsManager">
|
||||
<entry_points version="2.0" />
|
||||
</component>
|
||||
<component name="MavenImportPreferences">
|
||||
<option name="generalSettings">
|
||||
<MavenGeneralSettings>
|
||||
<option name="mavenHome" value="Bundled (Maven 3)" />
|
||||
</MavenGeneralSettings>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
|
||||
<OptionsSetting value="true" id="Add" />
|
||||
<OptionsSetting value="true" id="Remove" />
|
||||
<OptionsSetting value="true" id="Checkout" />
|
||||
<OptionsSetting value="true" id="Update" />
|
||||
<OptionsSetting value="true" id="Status" />
|
||||
<OptionsSetting value="true" id="Edit" />
|
||||
<ConfirmationsSetting value="0" id="Add" />
|
||||
<ConfirmationsSetting value="0" id="Remove" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/stFlashClient.iml" filepath="$PROJECT_DIR$/stFlashClient.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,29 @@
|
||||
import com.google.protobuf.ByteString
|
||||
import io.netty.buffer.Unpooled
|
||||
import io.netty.handler.codec.http.*
|
||||
import io.netty.util.CharsetUtil
|
||||
import proto.Carkot
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Created by user on 7/11/16.
|
||||
*/
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
//2 argm path to bin and server ip
|
||||
//1 option - 0x000...
|
||||
val host = "127.0.0.1"
|
||||
val port = 8888
|
||||
val base = "0x08000000"
|
||||
// val client = client.Client
|
||||
val bytesBinTest = Carkot.Upload.newBuilder().setData(ByteString.copyFrom(ByteArray(5, {x->0}))).setBase(base).build().toByteArray()
|
||||
|
||||
val request = DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/loadBin", Unpooled.copiedBuffer(bytesBinTest));
|
||||
request.headers().set(HttpHeaderNames.HOST, host)
|
||||
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE)
|
||||
request.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes())
|
||||
request.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8")
|
||||
|
||||
client.Client.sendRequest(request, host, port)
|
||||
println(client.ClientHandler.requestResult.code)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package client
|
||||
|
||||
import io.netty.bootstrap.Bootstrap
|
||||
import io.netty.channel.ChannelFutureListener
|
||||
import io.netty.channel.nio.NioEventLoopGroup
|
||||
import io.netty.channel.socket.nio.NioSocketChannel
|
||||
import io.netty.handler.codec.http.HttpRequest
|
||||
import io.netty.util.AttributeKey
|
||||
import java.net.ConnectException
|
||||
|
||||
/**
|
||||
* Created by user on 7/8/16.
|
||||
*/
|
||||
object Client {
|
||||
|
||||
fun sendRequest(request: HttpRequest, host: String, port: Int):Int {
|
||||
val group = NioEventLoopGroup()
|
||||
try {
|
||||
val bootstrap: Bootstrap = Bootstrap()
|
||||
bootstrap.group(group).channel(NioSocketChannel().javaClass).handler(ClientInitializer())
|
||||
val channelFuture = bootstrap.connect(host, port).sync()
|
||||
val channel = channelFuture.channel()
|
||||
channel.writeAndFlush(request)
|
||||
channel.closeFuture().sync()
|
||||
} catch (e: InterruptedException) {
|
||||
println("interrupted before request done")
|
||||
return 2
|
||||
} catch (e: ConnectException) {
|
||||
println("connection error to $host:$port")
|
||||
return 1
|
||||
} finally {
|
||||
group.shutdownGracefully()
|
||||
}
|
||||
return ClientHandler.requestResult.code
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package client
|
||||
|
||||
import com.google.protobuf.InvalidProtocolBufferException
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import io.netty.channel.SimpleChannelInboundHandler
|
||||
import io.netty.handler.codec.http.HttpContent
|
||||
import proto.Carkot
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Created by user on 7/8/16.
|
||||
*/
|
||||
class ClientHandler : SimpleChannelInboundHandler<Any> {
|
||||
|
||||
object requestResult {
|
||||
var code: Int = -1
|
||||
}
|
||||
|
||||
constructor()
|
||||
|
||||
var contentBytes: ByteArray = ByteArray(0);
|
||||
|
||||
override fun channelReadComplete(ctx: ChannelHandlerContext) {
|
||||
|
||||
var resultCode = 0
|
||||
try {
|
||||
val uploadResult: Carkot.UploadResult = Carkot.UploadResult.parseFrom(contentBytes)
|
||||
resultCode = uploadResult.resultCode
|
||||
} catch (e: InvalidProtocolBufferException) {
|
||||
e.printStackTrace()
|
||||
|
||||
resultCode = 2
|
||||
}
|
||||
synchronized(requestResult, {
|
||||
requestResult.code = resultCode
|
||||
})
|
||||
ctx.close()
|
||||
}
|
||||
|
||||
override fun channelRead0(ctx: ChannelHandlerContext?, msg: Any?) {
|
||||
if (msg is HttpContent) {
|
||||
val contentsBytes = msg.content();
|
||||
contentBytes = ByteArray(contentsBytes.capacity())
|
||||
contentsBytes.readBytes(contentBytes)
|
||||
}
|
||||
}
|
||||
|
||||
override fun exceptionCaught(ctx: ChannelHandlerContext?, cause: Throwable) {
|
||||
cause.printStackTrace()
|
||||
if (ctx != null) {
|
||||
ctx.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package client
|
||||
|
||||
import io.netty.channel.ChannelInitializer
|
||||
import io.netty.channel.socket.SocketChannel
|
||||
import io.netty.handler.codec.http.HttpClientCodec
|
||||
|
||||
/**
|
||||
* Created by user on 7/8/16.
|
||||
*/
|
||||
class ClientInitializer : ChannelInitializer<SocketChannel> {
|
||||
|
||||
constructor()
|
||||
|
||||
override fun initChannel(ch: SocketChannel) {
|
||||
val p = ch.pipeline()
|
||||
p.addLast(HttpClientCodec())
|
||||
p.addLast(ClientHandler())
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
|
||||
<orderEntry type="library" name="netty-all-4.1.2.Final-sources" level="project" />
|
||||
<orderEntry type="library" name="protobuf-java-3.0.0-beta-3" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -0,0 +1,4 @@
|
||||
.idea/workspace.xml
|
||||
.idea/uiDesigner.xml
|
||||
protobufjs
|
||||
out
|
||||
Generated
+13
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
|
||||
<OptionsSetting value="true" id="Add" />
|
||||
<OptionsSetting value="true" id="Remove" />
|
||||
<OptionsSetting value="true" id="Checkout" />
|
||||
<OptionsSetting value="true" id="Update" />
|
||||
<OptionsSetting value="true" id="Status" />
|
||||
<OptionsSetting value="true" id="Edit" />
|
||||
<ConfirmationsSetting value="0" id="Add" />
|
||||
<ConfirmationsSetting value="0" id="Remove" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/stFlashServer.iml" filepath="$PROJECT_DIR$/.idea/stFlashServer.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
Binary file not shown.
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Created by user on 7/11/16.
|
||||
*/
|
||||
const fs = require("fs");
|
||||
const protoBuf = require("./protobufjs");
|
||||
const builder = protoBuf.loadProtoFile("./proto/carkot.proto");//todo перенести как аргумент при запуске.
|
||||
const protoConstructor = builder.build("carkot");
|
||||
var commandPrefix = "./data/macosx-x86_64/st-flash write";
|
||||
var binFilePath = "./f.bin";
|
||||
|
||||
function loadBin(httpContent, response) {
|
||||
|
||||
var uploadClass = protoConstructor.Upload;
|
||||
var uploadObject = uploadClass.decode(httpContent);
|
||||
fs.writeFile(binFilePath, uploadObject.data.buffer, "binary", function (error) {
|
||||
var uploadResultClass = protoConstructor.UploadResult;
|
||||
var code = 0;
|
||||
if (error) {
|
||||
code = 2;
|
||||
} else {
|
||||
code = 0;
|
||||
console.log(commandPrefix + " " + binFilePath + " " + uploadObject.base);
|
||||
}
|
||||
var resultObject = new uploadResultClass({
|
||||
"stdOut": "",
|
||||
"resultCode": code,
|
||||
"stdErr": ""
|
||||
});
|
||||
var byteBuffer = resultObject.encode();
|
||||
var byteArray = [];
|
||||
for (var i = 0; i < byteBuffer.limit; i++) {
|
||||
byteArray.push(byteBuffer.buffer[i]);
|
||||
}
|
||||
response.writeHead(200, {"Content-Type": "text/plain", "Content-length": byteArray.length});
|
||||
response.write(new Buffer(byteArray));
|
||||
response.end();
|
||||
});
|
||||
}
|
||||
|
||||
function other(request, response) {
|
||||
console.log("other");
|
||||
response.writeHead(200, {"Content-Type": "text/plain"});
|
||||
response.end();
|
||||
}
|
||||
|
||||
|
||||
exports.loadBin = loadBin;
|
||||
exports.other = other;
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Created by user on 7/11/16.
|
||||
*/
|
||||
|
||||
var server = require("./server.js");
|
||||
var router = require("./router.js");
|
||||
var handlers = require("./handlers.js");
|
||||
|
||||
var handle = {};
|
||||
handle["/"] = handlers.other;
|
||||
handle["/loadBin"] = handlers.loadBin;
|
||||
handle["/other"] = handlers.other;
|
||||
|
||||
|
||||
// UploadR = upRes.UploadResult;
|
||||
// var res = new UploadR({"error": "test error!"});
|
||||
// console.log(res.encode());
|
||||
//
|
||||
//
|
||||
// var testBack = UploadR.decode(res.encode());
|
||||
// console.log(testBack);
|
||||
|
||||
// console.log(process.argv);
|
||||
server.start(router.route, handle);
|
||||
|
||||
// var http = require("http");
|
||||
//
|
||||
// var global_closure = null;
|
||||
//
|
||||
// // nodejs/javascript closures
|
||||
// http.createServer(function(request, response) {
|
||||
// // console.log("test");
|
||||
// global_closure && global_closure();
|
||||
//
|
||||
// // console.log(request);
|
||||
// // response.writeHead(200, {"Content-Type": "text/plain"});
|
||||
// // response.write("Hello World");
|
||||
//
|
||||
// function closure() {
|
||||
// console.log(request.toString());
|
||||
// }
|
||||
// global_closure = closure;
|
||||
//
|
||||
// //writeToJournal().getDBData().processDBData().sendResponse().commitJournal();
|
||||
// function writeToJournal() {
|
||||
// console.log(request);
|
||||
// getDBData(processDBData);
|
||||
// getDBData(function() {});
|
||||
// }
|
||||
//
|
||||
// function getDBData(callback) {
|
||||
// db.getData({ id : request.query['id'] }, onDbDataReady);
|
||||
// var i = 0;
|
||||
//
|
||||
// function onDbDataReady(err, someDBData) {
|
||||
// if (err) {
|
||||
// return response.error();
|
||||
// }
|
||||
// console.log(i);
|
||||
// callback(someDBData);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// function processDBData(dbData) {
|
||||
// function sendResponse() {
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// response.end();
|
||||
// }).listen(8888);
|
||||
@@ -0,0 +1,39 @@
|
||||
syntax = "proto3";
|
||||
package carkot;
|
||||
|
||||
option java_package = "proto";
|
||||
option java_outer_classname = "Carkot";
|
||||
|
||||
message Upload {
|
||||
message Method {
|
||||
enum TYPE {
|
||||
DFU = 0;
|
||||
STLINK = 1;
|
||||
}
|
||||
// Type of upload method.
|
||||
TYPE type = 1;
|
||||
// Upload port.
|
||||
string port = 2;
|
||||
// Device address, smth like 8087:0ABA.
|
||||
string device = 3;
|
||||
// Additional programmator-specific arguments.
|
||||
repeated string arguments = 4;
|
||||
}
|
||||
// Firmware data.
|
||||
bytes data = 1;
|
||||
// Firmware load address (string, to avoid 64-bit woes).
|
||||
string base = 2;
|
||||
// Method of firmware upload.
|
||||
Method method = 3;
|
||||
}
|
||||
|
||||
message UploadResult {
|
||||
string stdOut = 1;
|
||||
string stdErr = 2;
|
||||
int32 resultCode = 3;
|
||||
}
|
||||
|
||||
message LogMessage {
|
||||
string source = 1;
|
||||
bytes message = 2;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Created by user on 7/11/16.
|
||||
*/
|
||||
function route(handle, pathname, httpContent, response) {
|
||||
if (typeof handle[pathname] === 'function') {
|
||||
handle[pathname](httpContent, response);
|
||||
} else {
|
||||
console.log("No request handler found for " + pathname);
|
||||
response.writeHead(404, {"Content-Type": "text/plain"});
|
||||
response.write("404 Not found");
|
||||
response.end();
|
||||
}
|
||||
}
|
||||
|
||||
exports.route = route;
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Created by user on 7/11/16.
|
||||
*/
|
||||
var http = require("http");
|
||||
var url = require("url");
|
||||
|
||||
function start(route, handlers) {
|
||||
function onRequest(request, response) {
|
||||
var pathname = url.parse(request.url).pathname;
|
||||
var httpContent = [];
|
||||
request.on("data", function (datas) {
|
||||
var strDatas = datas.toString();
|
||||
for (var i = 0; i < datas.length; ++i) {
|
||||
httpContent.push(strDatas.charCodeAt(i));
|
||||
}
|
||||
});
|
||||
request.on("end", function () {
|
||||
route(handlers, pathname, httpContent, response);
|
||||
});
|
||||
}
|
||||
|
||||
http.createServer(onRequest).listen(8888);
|
||||
console.log("Server has started.");
|
||||
}
|
||||
exports.start = start;
|
||||
Reference in New Issue
Block a user