/
vivadoserver.tcl
182 lines (154 loc) · 6.44 KB
/
vivadoserver.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#
# Copyright 2017 Matthijs Bos <matthijs_vlaarbos@hotmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package require Tcl 8.5
package require base64 2.4
package require fileutil 1.13.5
package require fpgaedu::jsonrpc 1.0
package require fpgaedu::json 2.0
package require fpgaedu::vivado 1.0
package provide fpgaedu::vivadoserver 1.0
namespace eval ::fpgaedu::vivadoserver {
namespace export vivadoserver
namespace import ::fpgaedu::jsonrpc::jsonrpc
namespace import ::fpgaedu::json::json
namespace import ::fpgaedu::vivado::vivado
# variable to signal event loop cancellation
variable listening 1
# static rpc handler configuration
variable rpcConfig
jsonrpc map rpcConfig echo ::fpgaedu::vivadoserver::RpcEchoHandler
jsonrpc map rpcConfig exit ::fpgaedu::vivadoserver::RpcExitHandler
jsonrpc map rpcConfig program ::fpgaedu::vivadoserver::RpcProgramHandler
jsonrpc map rpcConfig getTargetIdentifiers ::fpgaedu::vivadoserver::RpcGetTargetIdentifiersHandler
jsonrpc map rpcConfig getDeviceIdentifiers ::fpgaedu::vivadoserver::RpcGetDeviceIdentifiersHandler
# namespace ensemble configuration
set commandMap {
start ::fpgaedu::vivadoserver::Start
}
namespace ensemble create \
-map $commandMap
namespace ensemble create \
-command vivadoserver \
-map $commandMap
}
proc ::fpgaedu::vivadoserver::RpcEchoHandler {paramsJson} {
return $paramsJson
}
proc ::fpgaedu::vivadoserver::RpcExitHandler {paramsJson} {
variable listening
set listening 0
return [json create null]
}
proc ::fpgaedu::vivadoserver::RpcGetTargetIdentifiersHandler {paramsJson} {
set targets [fpgaedu::vivado getTargetIdentifiers]
set resultJson [json create array]
foreach target $targets {
json append resultJson string $target
}
return $resultJson
}
proc ::fpgaedu::vivadoserver::RpcGetDeviceIdentifiersHandler {paramsJson} {
if {![json contains $paramsJson -key targetIdentifier]} {
jsonrpc throw \
-code invalidParams \
-message "Missing param targetIdentifier"
} elseif {![json contains $paramsJson -key targetIdentifier -type string]} {
jsonrpc throw \
-code invalidParams \
-message "Invalid type for param targetIdentifier, must be string"
}
set targetIdentifier [json get $paramsJson targetIdentifier]
set devices [fpgaedu::vivado getDeviceIdentifiers $targetIdentifier]
set resultJson [json create array]
foreach device $devices {
json append resultJson string $device
}
return $resultJson
}
proc ::fpgaedu::vivadoserver::RpcProgramHandler {paramsJson} {
# validate params
if {![json contains $paramsJson -key target]} {
jsonrpc throw \
-code invalidParams \
-message "Missing param target"
} elseif {![json contains $paramsJson -key target -type string]} {
jsonrpc throw \
-code invalidParams \
-message "Invalid type for param target, must be string"
} elseif {![json contains $paramsJson -key device]} {
jsonrpc throw \
-code invalidParams \
-message "Missing param device"
} elseif {![json contains $paramsJson -key device -type string]} {
jsonrpc throw \
-code invalidParams \
-message "Invalid type for param device, must be string"
} elseif {![json contains $paramsJson -key bitstream]} {
jsonrpc throw \
-code invalidParams \
-message "Missing param bitstream"
} elseif {![json contains $paramsJson -key bitstream -type string]} {
jsonrpc throw \
-code invalidParams \
-message "Invalid type for param bitstream, must be string"
}
# Extract the base64 encoded bitstream from the request parameters and write
# the decoded contents to a temporary file.
# Vivado requires the bitstream file have a .bit extension. Since the
# tempfile procedure does not allow to define a prefix, this is done
# manually by renaming the file.
# The request base64 bitstream contents are written in binary format.
set bitstreamBase64 [json get $paramsJson bitstream]
set bitstreamBinary [::base64::decode $bitstreamBase64]
set bitstreamFilePath [::fileutil::tempfile bitstream]
file rename $bitstreamFilePath "$bitstreamFilePath.bit"
set bitstreamFilePath "$bitstreamFilePath.bit"
set bitstreamFileChan [open $bitstreamFilePath w]
chan configure $bitstreamFileChan -encoding binary -translation binary
chan puts -nonewline $bitstreamFileChan $bitstreamBinary
chan close $bitstreamFileChan
# Extract additional params
set target [json get $paramsJson target]
set device [json get $paramsJson device]
# program the actual device
vivado program $bitstreamFilePath $target $device
# return json null
return [json create null]
}
proc ::fpgaedu::vivadoserver::SocketHandler {channelId} {
variable rpcConfig
catch {
set requestData [chan read $channelId]
set responseData [jsonrpc handle $rpcConfig $requestData]
chan puts -nonewline $channelId $responseData
}
chan close $channelId
}
# http://wiki.tcl.tk/5947
proc ::fpgaedu::vivadoserver::AcceptConnection {channelId addr port} {
fconfigure $channelId -blocking 1 -buffering full
fileevent $channelId readable [list ::fpgaedu::vivadoserver::SocketHandler $channelId]
}
proc ::fpgaedu::vivadoserver::Start {{port 3742}} {
variable listening
# Registering an empty fileevent handler for stdin allows for the reading of
# SIGINT while the event loop is running.
fileevent stdin readable {}
puts "Starting server. Press Ctrl-C to exit"
# Setup the socket connection handler and start the event loop.
socket -server AcceptConnection $port
vwait listening
}