openc3 7.5.0 → 7.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -0
  3. data/bin/openc3cli +82 -5
  4. data/data/config/_params.yaml +3 -0
  5. data/data/config/command_modifiers.yaml +5 -3
  6. data/data/config/default_plugins.yaml +1 -0
  7. data/data/config/interface_modifiers.yaml +17 -1
  8. data/data/config/item_modifiers.yaml +24 -1
  9. data/data/config/microservice.yaml +15 -1
  10. data/data/config/parameter_modifiers.yaml +4 -4
  11. data/data/config/plugins.yaml +511 -3
  12. data/data/config/settings.yaml +17 -0
  13. data/data/config/target.yaml +53 -0
  14. data/data/config/telemetry.yaml +58 -2
  15. data/data/config/widgets.yaml +30 -0
  16. data/lib/openc3/accessors/accessor.rb +14 -2
  17. data/lib/openc3/accessors/binary_accessor.rb +25 -5
  18. data/lib/openc3/api/CLAUDE.md +1 -0
  19. data/lib/openc3/api/api.rb +1 -0
  20. data/lib/openc3/api/cmd_api.rb +9 -1
  21. data/lib/openc3/api/interface_api.rb +7 -2
  22. data/lib/openc3/api/limits_api.rb +236 -195
  23. data/lib/openc3/api/router_api.rb +7 -2
  24. data/lib/openc3/api/settings_api.rb +4 -1
  25. data/lib/openc3/api/shared_resource_api.rb +107 -0
  26. data/lib/openc3/api/tlm_api.rb +229 -61
  27. data/lib/openc3/interfaces/http_client_interface.rb +84 -27
  28. data/lib/openc3/interfaces/protocols/preidentified_protocol.rb +6 -2
  29. data/lib/openc3/interfaces/tcpip_server_interface.rb +205 -125
  30. data/lib/openc3/io/json_api.rb +2 -10
  31. data/lib/openc3/io/json_api_object.rb +2 -10
  32. data/lib/openc3/logs/log_writer.rb +9 -8
  33. data/lib/openc3/logs/packet_log_writer.rb +8 -3
  34. data/lib/openc3/microservices/decom_common.rb +4 -1
  35. data/lib/openc3/microservices/decom_microservice.rb +73 -5
  36. data/lib/openc3/microservices/interface_decom_common.rb +16 -1
  37. data/lib/openc3/microservices/interface_microservice.rb +50 -11
  38. data/lib/openc3/microservices/limits_group_microservice.rb +513 -0
  39. data/lib/openc3/microservices/log_microservice.rb +22 -8
  40. data/lib/openc3/microservices/microservice.rb +8 -2
  41. data/lib/openc3/microservices/multi_microservice.rb +76 -22
  42. data/lib/openc3/microservices/queue_microservice.rb +29 -24
  43. data/lib/openc3/migrations/20260704000000_default_bridge.rb +1 -2
  44. data/lib/openc3/migrations/20261103000001_limits_group_backfill.rb +193 -0
  45. data/lib/openc3/models/bridge_model.rb +3 -3
  46. data/lib/openc3/models/cvt_model.rb +10 -3
  47. data/lib/openc3/models/host_interface_microservice_model.rb +4 -4
  48. data/lib/openc3/models/interface_model.rb +27 -7
  49. data/lib/openc3/models/microservice_group.rb +167 -0
  50. data/lib/openc3/models/microservice_model.rb +88 -2
  51. data/lib/openc3/models/panel_model.rb +109 -0
  52. data/lib/openc3/models/plugin_model.rb +1006 -26
  53. data/lib/openc3/models/plugin_store_model.rb +6 -1
  54. data/lib/openc3/models/protected_storage_model.rb +64 -0
  55. data/lib/openc3/models/queue_model.rb +19 -10
  56. data/lib/openc3/models/scope_model.rb +80 -3
  57. data/lib/openc3/models/setting_model.rb +2 -2
  58. data/lib/openc3/models/shared_resource_model.rb +381 -0
  59. data/lib/openc3/models/target_model.rb +333 -74
  60. data/lib/openc3/operators/microservice_operator.rb +76 -9
  61. data/lib/openc3/operators/operator.rb +29 -0
  62. data/lib/openc3/packets/commands.rb +16 -16
  63. data/lib/openc3/packets/limits.rb +2 -0
  64. data/lib/openc3/packets/packet.rb +75 -4
  65. data/lib/openc3/packets/packet_config.rb +152 -13
  66. data/lib/openc3/packets/packet_item.rb +48 -22
  67. data/lib/openc3/packets/packet_item_limits.rb +67 -6
  68. data/lib/openc3/packets/parsers/limits_parser.rb +69 -2
  69. data/lib/openc3/packets/parsers/packet_item_parser.rb +6 -0
  70. data/lib/openc3/packets/parsers/packet_parser.rb +45 -0
  71. data/lib/openc3/packets/parsers/state_parser.rb +1 -1
  72. data/lib/openc3/script/CLAUDE.md +1 -0
  73. data/lib/openc3/script/api_shared.rb +27 -17
  74. data/lib/openc3/script/calendar.rb +18 -6
  75. data/lib/openc3/script/commands.rb +11 -9
  76. data/lib/openc3/script/critical_cmd.rb +22 -2
  77. data/lib/openc3/script/extract.rb +52 -5
  78. data/lib/openc3/script/metadata.rb +30 -4
  79. data/lib/openc3/script/plugins.rb +8 -0
  80. data/lib/openc3/script/script.rb +9 -25
  81. data/lib/openc3/script/web_socket_api.rb +23 -17
  82. data/lib/openc3/system/system.rb +9 -3
  83. data/lib/openc3/top_level.rb +17 -0
  84. data/lib/openc3/topics/command_topic.rb +7 -1
  85. data/lib/openc3/topics/decom_interface_topic.rb +9 -3
  86. data/lib/openc3/topics/discovery_topic.rb +84 -0
  87. data/lib/openc3/topics/limits_event_topic.rb +317 -11
  88. data/lib/openc3/topics/router_topic.rb +6 -2
  89. data/lib/openc3/topics/system_events_topic.rb +9 -6
  90. data/lib/openc3/topics/telemetry_decom_topic.rb +12 -3
  91. data/lib/openc3/topics/topic.rb +9 -1
  92. data/lib/openc3/utilities/authentication.rb +67 -16
  93. data/lib/openc3/utilities/bucket.rb +5 -0
  94. data/lib/openc3/utilities/cli_generator.rb +117 -25
  95. data/lib/openc3/utilities/client_ip_header_filter.rb +38 -0
  96. data/lib/openc3/utilities/config_overlay.rb +24 -1
  97. data/lib/openc3/utilities/cosmos_rails_formatter.rb +18 -10
  98. data/lib/openc3/utilities/hardware_discovery.rb +279 -0
  99. data/lib/openc3/utilities/limits_rollup.rb +81 -0
  100. data/lib/openc3/utilities/local_mode.rb +48 -5
  101. data/lib/openc3/utilities/questdb_client.rb +256 -54
  102. data/lib/openc3/utilities/reingest_job.rb +12 -3
  103. data/lib/openc3/utilities/running_script.rb +206 -49
  104. data/lib/openc3/utilities/script.rb +24 -12
  105. data/lib/openc3/utilities/script_env.rb +164 -0
  106. data/lib/openc3/utilities/script_message_signing.rb +133 -0
  107. data/lib/openc3/utilities/secrets.rb +5 -0
  108. data/lib/openc3/utilities/store_autoload.rb +35 -2
  109. data/lib/openc3/utilities/store_queued.rb +5 -1
  110. data/lib/openc3/version.rb +5 -5
  111. data/templates/command_validator/command_validator.py +9 -9
  112. data/templates/{tool_react/pnpm-workspace.yaml → panel/.prettierrc.cjs} +7 -5
  113. data/templates/panel/Rakefile +24 -0
  114. data/templates/panel/package.json +17 -0
  115. data/templates/{tool_angular → panel}/pnpm-workspace.yaml +5 -0
  116. data/templates/panel/src/Panel.js +52 -0
  117. data/templates/panel/src/Panel.vue +60 -0
  118. data/templates/panel/src/panelSdk.js +24 -0
  119. data/templates/panel/vite-Panel.config.js +43 -0
  120. data/templates/tool_vue/package.json +3 -3
  121. data/templates/widget/package.json +2 -2
  122. metadata +24 -56
  123. data/templates/tool_angular/.editorconfig +0 -16
  124. data/templates/tool_angular/.gitignore +0 -44
  125. data/templates/tool_angular/.vscode/extensions.json +0 -4
  126. data/templates/tool_angular/.vscode/launch.json +0 -20
  127. data/templates/tool_angular/.vscode/tasks.json +0 -42
  128. data/templates/tool_angular/angular.json +0 -111
  129. data/templates/tool_angular/extra-webpack.config.js +0 -8
  130. data/templates/tool_angular/package.json +0 -48
  131. data/templates/tool_angular/src/app/app-routing.module.ts +0 -15
  132. data/templates/tool_angular/src/app/app.component.html +0 -22
  133. data/templates/tool_angular/src/app/app.component.scss +0 -18
  134. data/templates/tool_angular/src/app/app.component.spec.ts +0 -29
  135. data/templates/tool_angular/src/app/app.component.ts +0 -52
  136. data/templates/tool_angular/src/app/app.module.ts +0 -30
  137. data/templates/tool_angular/src/app/custom-overlay-container.ts +0 -17
  138. data/templates/tool_angular/src/app/empty-route/empty-route.component.ts +0 -7
  139. data/templates/tool_angular/src/app/openc3-api.d.ts +0 -1
  140. data/templates/tool_angular/src/assets/.gitkeep +0 -0
  141. data/templates/tool_angular/src/environments/environment.prod.ts +0 -3
  142. data/templates/tool_angular/src/environments/environment.ts +0 -16
  143. data/templates/tool_angular/src/favicon.ico +0 -0
  144. data/templates/tool_angular/src/index.html +0 -13
  145. data/templates/tool_angular/src/main.single-spa.ts +0 -40
  146. data/templates/tool_angular/src/single-spa/asset-url.ts +0 -12
  147. data/templates/tool_angular/src/single-spa/single-spa-props.ts +0 -8
  148. data/templates/tool_angular/src/styles.scss +0 -1
  149. data/templates/tool_angular/tsconfig.app.json +0 -13
  150. data/templates/tool_angular/tsconfig.json +0 -33
  151. data/templates/tool_angular/tsconfig.spec.json +0 -14
  152. data/templates/tool_react/.eslintrc +0 -7
  153. data/templates/tool_react/.gitignore +0 -71
  154. data/templates/tool_react/.prettierignore +0 -8
  155. data/templates/tool_react/babel.config.json +0 -29
  156. data/templates/tool_react/jest.config.js +0 -12
  157. data/templates/tool_react/package.json +0 -51
  158. data/templates/tool_react/src/openc3-tool_name.js +0 -24
  159. data/templates/tool_react/src/root.component.js +0 -88
  160. data/templates/tool_react/src/root.component.test.js +0 -9
  161. data/templates/tool_react/webpack.config.js +0 -27
  162. data/templates/tool_svelte/.gitignore +0 -71
  163. data/templates/tool_svelte/.prettierignore +0 -8
  164. data/templates/tool_svelte/.prettierrc.js +0 -5
  165. data/templates/tool_svelte/babel.config.js +0 -12
  166. data/templates/tool_svelte/build/smui.css +0 -1
  167. data/templates/tool_svelte/jest.config.js +0 -9
  168. data/templates/tool_svelte/package.json +0 -49
  169. data/templates/tool_svelte/pnpm-workspace.yaml +0 -20
  170. data/templates/tool_svelte/rollup.config.js +0 -74
  171. data/templates/tool_svelte/src/App.svelte +0 -42
  172. data/templates/tool_svelte/src/App.test.js +0 -9
  173. data/templates/tool_svelte/src/theme/_smui-theme.scss +0 -25
  174. data/templates/tool_svelte/src/tool_name.js +0 -17
  175. data/templates/widget/LICENSE.md +0 -20
@@ -0,0 +1,107 @@
1
+ # encoding: ascii-8bit
2
+
3
+ # Copyright 2026 OpenC3, Inc.
4
+ # All Rights Reserved.
5
+ #
6
+ # This program is distributed in the hope that it will be useful,
7
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
8
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
+ # See LICENSE.md for more details.
10
+
11
+ # This file may also be used under the terms of a commercial license
12
+ # if purchased from OpenC3, Inc.
13
+
14
+ require 'openc3/models/shared_resource_model'
15
+
16
+ module OpenC3
17
+ module Api
18
+ WHITELIST ||= []
19
+ WHITELIST.concat([
20
+ 'get_shared_resources',
21
+ 'get_shared_resource',
22
+ 'switch_shared_resource',
23
+ 'release_shared_resource',
24
+ ])
25
+
26
+ # Members in scopes the user can't access are left out
27
+ # @return [Array<Hash>] The shared resources with members in the scope
28
+ def get_shared_resources(manual: false, scope: $openc3_scope, token: $openc3_token)
29
+ authorize(permission: 'system', manual: manual, scope: scope, token: token)
30
+ records = SharedResourceModel.all.values.select { |record| record['members'][scope] }.sort_by { |record| record['name'] }
31
+ accessible = { scope => true }
32
+ records.map { |record| _shared_resource_for_user(record, accessible, manual: manual, token: token) }
33
+ end
34
+
35
+ # Members in scopes the user can't access are left out
36
+ # @param name [String] Shared resource name
37
+ # @return [Hash|nil] The shared resource or nil if it doesn't exist or has no members in the scope
38
+ def get_shared_resource(name, manual: false, scope: $openc3_scope, token: $openc3_token)
39
+ authorize(permission: 'system', manual: manual, scope: scope, token: token)
40
+ record = SharedResourceModel.get(name)
41
+ return nil unless record and record['members'][scope]
42
+ _shared_resource_for_user(record, { scope => true }, manual: manual, token: token)
43
+ end
44
+
45
+ # Make a shared resource active in a scope. Its microservices in the
46
+ # currently active scope are stopped before those in the new scope start.
47
+ #
48
+ # @param name [String] Shared resource name
49
+ # @param to_scope [String] Scope to make the resource active in (default is the current scope)
50
+ # @param force [Boolean] Don't wait for the active scope's microservices to stop
51
+ # @return [Hash] The updated shared resource
52
+ def switch_shared_resource(name, to_scope = nil, force: false, manual: false, scope: $openc3_scope, token: $openc3_token)
53
+ to_scope ||= scope
54
+ # Switching affects both scopes so require admin in each
55
+ authorize(permission: 'admin', manual: manual, scope: to_scope, token: token)
56
+ record = SharedResourceModel.get(name)
57
+ raise "Shared resource '#{name.to_s.upcase}' does not exist" unless record
58
+ if record['owner'] and record['owner'] != to_scope
59
+ authorize(permission: 'admin', manual: manual, scope: record['owner'], token: token)
60
+ end
61
+ record = SharedResourceModel.switch(name, to_scope: to_scope, force: force)
62
+ username = user_info(token)['username'] || 'Anonymous'
63
+ Logger.info("User #{username} switched shared resource #{record['name']} to scope #{to_scope}#{force ? ' (forced)' : ''}", scope: to_scope, user: username)
64
+ record
65
+ end
66
+
67
+ # Release a shared resource so it is not active in any scope
68
+ #
69
+ # @param name [String] Shared resource name
70
+ # @param force [Boolean] Don't wait for the active scope's microservices to stop
71
+ # @return [Hash] The updated shared resource
72
+ def release_shared_resource(name, force: false, manual: false, scope: $openc3_scope, token: $openc3_token)
73
+ authorize(permission: 'admin', manual: manual, scope: scope, token: token)
74
+ record = SharedResourceModel.get(name)
75
+ raise "Shared resource '#{name.to_s.upcase}' does not exist" unless record
76
+ if record['owner'] and record['owner'] != scope
77
+ authorize(permission: 'admin', manual: manual, scope: record['owner'], token: token)
78
+ end
79
+ record = SharedResourceModel.release(name, force: force)
80
+ username = user_info(token)['username'] || 'Anonymous'
81
+ Logger.info("User #{username} released shared resource #{record['name']}#{force ? ' (forced)' : ''}", scope: scope, user: username)
82
+ record
83
+ end
84
+
85
+ # Remove the members (and the owner's waiting members) of scopes the user
86
+ # doesn't have system permission in. The owner and state stay so the UI can
87
+ # say where the resource is active.
88
+ # @param accessible [Hash<String, Boolean>] Cache of scope to whether the user can access it
89
+ def _shared_resource_for_user(record, accessible, manual:, token:)
90
+ can_access = lambda do |other_scope|
91
+ unless accessible.key?(other_scope)
92
+ begin
93
+ authorize(permission: 'system', manual: manual, scope: other_scope, token: token)
94
+ accessible[other_scope] = true
95
+ rescue ForbiddenError
96
+ accessible[other_scope] = false
97
+ end
98
+ end
99
+ accessible[other_scope]
100
+ end
101
+ record = record.dup
102
+ record['members'] = record['members'].select { |member_scope, _members| can_access.call(member_scope) }
103
+ record['waiting'] = [] if record['owner'] and record['waiting'] and !can_access.call(record['owner'])
104
+ record
105
+ end
106
+ end
107
+ end
@@ -119,6 +119,8 @@ module OpenC3
119
119
  # @param type [Symbol] Telemetry type, :RAW, :CONVERTED (default), :FORMATTED
120
120
  # @param received_time [Integer, nil] Optional received time as nanoseconds since Unix epoch
121
121
  def inject_tlm(target_name, packet_name, item_hash = nil, type: :CONVERTED, stored: false, received_time: nil, manual: false, scope: $openc3_scope, token: $openc3_token)
122
+ _tlm_reject_latest(packet_name.to_s.upcase, 'inject_tlm')
123
+ _tlm_reject_read_only(target_name.to_s.upcase, packet_name.to_s.upcase, 'inject_tlm')
122
124
  authorize(permission: 'tlm_set', target_name: target_name, packet_name: packet_name, manual: manual, scope: scope, token: token)
123
125
  type = type.to_s.intern
124
126
  target_name = target_name.upcase
@@ -174,12 +176,17 @@ module OpenC3
174
176
  #
175
177
  # @param args The args must either be a string followed by a value or
176
178
  # three strings followed by a value (see the calling style in the
177
- # description).
179
+ # description). A packet name of LATEST overrides the item in every
180
+ # packet which contains it.
178
181
  # @param type [Symbol] Telemetry type, :ALL (default), :RAW, :CONVERTED, :FORMATTED
179
182
  def override_tlm(*args, type: :ALL, manual: false, scope: $openc3_scope, token: $openc3_token)
180
- target_name, packet_name, item_name, value = _set_tlm_process_args(args, __method__, scope: scope)
181
- authorize(permission: 'tlm_set', target_name: target_name, packet_name: packet_name, manual: manual, scope: scope, token: token)
182
- CvtModel.override(target_name, packet_name, item_name, value, type: type.intern, scope: scope)
183
+ target_name, packet_names, item_name, value = _set_tlm_process_args_all_packets(args, __method__, scope: scope)
184
+ packet_names.each do |packet_name|
185
+ authorize(permission: 'tlm_set', target_name: target_name, packet_name: packet_name, manual: manual, scope: scope, token: token)
186
+ end
187
+ packet_names.each do |packet_name|
188
+ CvtModel.override(target_name, packet_name, item_name, value, type: type.intern, scope: scope)
189
+ end
183
190
  end
184
191
 
185
192
  # Get the list of CVT overrides
@@ -198,13 +205,18 @@ module OpenC3
198
205
  # Favor the first syntax where possible as it is more succinct.
199
206
  #
200
207
  # @param args The args must either be a string or three strings
201
- # (see the calling style in the description).
208
+ # (see the calling style in the description). A packet name of LATEST
209
+ # normalizes the item in every packet which contains it.
202
210
  # @param type [Symbol] Telemetry type, :ALL (default), :RAW, :CONVERTED, :FORMATTED
203
211
  # Also takes :ALL which means to normalize all telemetry types
204
212
  def normalize_tlm(*args, type: :ALL, manual: false, scope: $openc3_scope, token: $openc3_token)
205
- target_name, packet_name, item_name = _tlm_process_args(args, __method__, scope: scope)
206
- authorize(permission: 'tlm_set', target_name: target_name, packet_name: packet_name, manual: manual, scope: scope, token: token)
207
- CvtModel.normalize(target_name, packet_name, item_name, type: type.intern, scope: scope)
213
+ target_name, packet_names, item_name = _tlm_process_args_all_packets(args, __method__, scope: scope)
214
+ packet_names.each do |packet_name|
215
+ authorize(permission: 'tlm_set', target_name: target_name, packet_name: packet_name, manual: manual, scope: scope, token: token)
216
+ end
217
+ packet_names.each do |packet_name|
218
+ CvtModel.normalize(target_name, packet_name, item_name, type: type.intern, scope: scope)
219
+ end
208
220
  end
209
221
 
210
222
  # Returns the raw buffer for a telemetry packet.
@@ -265,45 +277,25 @@ module OpenC3
265
277
  packet_name = orig_packet_name
266
278
  raise ArgumentError, "items must be formatted as TGT__PKT__ITEM__TYPE" if target_name.nil? || packet_name.nil? || item_name.nil? || value_type.nil?
267
279
  if orig_packet_name == 'LATEST'
268
- # TODO: Do we need to lookup ALL the possible packets for this item?
280
+ # A LATEST item can come from any packet containing it. Report the best value
281
+ # type and limits of any of them since the CVT falls back to the available
282
+ # type and historical lookups check the type available in each packet.
269
283
  # We can have a large cache_timeout of 1 because all we're trying to do is lookup a packet
270
284
  packet_name = CvtModel.determine_latest_packet_for_item(target_name, item_name, cache_timeout: 1, scope: scope)
285
+ packet_names = _tlm_packet_names(target_name, orig_packet_name, item_name, scope: scope)
286
+ else
287
+ packet_names = [packet_name]
288
+ end
289
+ # Authorize every packet whose definition is read below, not just the latest one
290
+ (packet_names | [packet_name]).each do |name|
291
+ authorize(permission: 'tlm', target_name: target_name, packet_name: name, scope: scope, token: token)
271
292
  end
272
- authorize(permission: 'tlm', target_name: target_name, packet_name: packet_name, scope: scope, token: token)
273
293
  begin
274
- item = TargetModel.packet_item(target_name, packet_name, item_name, scope: scope)
275
- if Packet::RESERVED_ITEM_NAMES.include?(item_name)
276
- value_type = 'RAW' # Must request the raw value when dealing with the reserved items
277
- end
278
-
279
- # Arrays must be accessed as RAW since there's no conversion
280
- if item['array_size']
281
- value_type = 'RAW'
282
- end
283
-
284
- case value_type
285
- when 'FORMATTED', 'WITH_UNITS'
286
- if item['format_string'] or item['units']
287
- results << [target_name, orig_packet_name, item_name, 'FORMATTED'].join('__')
288
- # This logic must match the logic in Packet#decom
289
- elsif item['states'] or (item['read_conversion'] and item['data_type'] != 'DERIVED')
290
- results << [target_name, orig_packet_name, item_name, 'CONVERTED'].join('__')
291
- else
292
- results << [target_name, orig_packet_name, item_name, 'RAW'].join('__')
293
- end
294
- when 'CONVERTED'
295
- # This logic must match the logic in Packet#decom
296
- if item['states'] or (item['read_conversion'] and item['data_type'] != 'DERIVED')
297
- results << [target_name, orig_packet_name, item_name, 'CONVERTED'].join('__')
298
- else
299
- results << [target_name, orig_packet_name, item_name, 'RAW'].join('__')
300
- end
301
- else # RAW or unknown
302
- results << [target_name, orig_packet_name, item_name, 'RAW'].join('__')
303
- end
304
-
294
+ packet_items = packet_names.map { |name| TargetModel.packet_item(target_name, name, item_name, scope: scope) }
295
+ available_type = packet_items.map { |packet_item| _tlm_available_value_type(packet_item, item_name, value_type) }.max_by { |type| VALUE_TYPE_ORDER.index(type) }
296
+ results << [target_name, orig_packet_name, item_name, available_type].join('__')
305
297
  # Tack on __LIMITS to notify that we have an available limits value
306
- if item['limits']['DEFAULT']
298
+ if packet_items.any? { |packet_item| packet_item['limits']['DEFAULT'] }
307
299
  results[-1] += '__LIMITS'
308
300
  end
309
301
  rescue RuntimeError
@@ -341,7 +333,20 @@ module OpenC3
341
333
  parts = item.to_s.upcase.split('__')
342
334
  raise ArgumentError, "items must be formatted as TGT__PKT__ITEM__TYPE" if parts.length < 4 || parts.length > 5
343
335
  target_name, packet_name, item_name, value_type, limits = parts
344
- if packet_name == 'LATEST' # Lookup packet_name in case of LATEST
336
+ if packet_name == 'LATEST'
337
+ if start_time
338
+ # The packet received most recently now isn't necessarily the one received most
339
+ # recently at start_time so keep every candidate packet for _tlm_latest_history
340
+ # Each packet must be queried for the value type and limits it has or the query fails
341
+ candidates = _tlm_packet_names(target_name, packet_name, item_name, scope: scope).map do |candidate|
342
+ packets << [target_name, candidate]
343
+ candidate_item = TargetModel.packet_item(target_name, candidate, item_name, scope: scope)
344
+ candidate_limits = (limits and candidate_item['limits']['DEFAULT']) ? limits : nil
345
+ [target_name, candidate, item_name, _tlm_available_value_type(candidate_item, item_name, value_type), candidate_limits]
346
+ end
347
+ cvt_items << [target_name, packet_name, item_name, value_type, limits, candidates]
348
+ next
349
+ end
345
350
  packet_name = CvtModel.determine_latest_packet_for_item(target_name, item_name, cache_timeout: cache_timeout, scope: scope)
346
351
  end
347
352
  # NOTE: limits is required by the historical (start_time) QuestDB lookup
@@ -352,6 +357,9 @@ module OpenC3
352
357
  packets.each do |target_name, packet_name|
353
358
  authorize(permission: 'tlm', target_name: target_name, packet_name: packet_name, manual: manual, scope: scope, token: token)
354
359
  end
360
+ if cvt_items.any? { |cvt_item| cvt_item.length == 6 }
361
+ return _tlm_latest_history(cvt_items, start_time: start_time, end_time: end_time, cache_timeout: cache_timeout, scope: scope)
362
+ end
355
363
  CvtModel.get_tlm_values(cvt_items, stale_time: stale_time, cache_timeout: cache_timeout, start_time: start_time, end_time: end_time, scope: scope)
356
364
  end
357
365
 
@@ -446,6 +454,7 @@ module OpenC3
446
454
  packets.each do |target_name, packet_name|
447
455
  target_name = target_name.upcase
448
456
  packet_name = packet_name.upcase
457
+ _tlm_reject_latest(packet_name, 'subscribe_packets')
449
458
  authorize(permission: 'tlm', target_name: target_name, packet_name: packet_name, manual: manual, scope: scope, token: token)
450
459
  topic = "#{scope}__DECOM__{#{target_name}}__#{packet_name}"
451
460
  db_shard = Store.db_shard_for_target(target_name, scope: scope)
@@ -475,7 +484,7 @@ module OpenC3
475
484
  # Group topics by db_shard for multi-shard support
476
485
  db_shard_groups = {}
477
486
  lookup.each do |topic, offset|
478
- target_name = topic.match(/__\{?([^}_]+)\}?__/)[1] rescue nil
487
+ target_name = Topic.target_name_from_topic(topic)
479
488
  db_shard = Store.db_shard_for_target(target_name, scope: scope)
480
489
  db_shard_groups[db_shard] ||= { topics: [], offsets: [] }
481
490
  db_shard_groups[db_shard][:topics] << topic
@@ -551,9 +560,22 @@ module OpenC3
551
560
  if target_name.nil? or packet_name.nil?
552
561
  raise "ERROR: Both target name and packet name required. Usage: #{method_name}(\"TGT PKT\") or #{method_name}(\"TGT\", \"PKT\")"
553
562
  end
563
+ _tlm_reject_latest(packet_name, method_name)
554
564
  return [target_name, packet_name]
555
565
  end
556
566
 
567
+ # LATEST picks a packet per item so it can't identify a whole packet
568
+ def _tlm_reject_latest(packet_name, method_name)
569
+ raise "ERROR: LATEST is not supported by #{method_name}(), specify a packet name" if packet_name == 'LATEST'
570
+ end
571
+
572
+ # <TARGET> LIMITS_GROUP is computed by the limits group microservice so it can't be written
573
+ def _tlm_reject_read_only(target_name, packet_name, method_name)
574
+ if packet_name == PacketConfig::LIMITS_GROUP_PACKET
575
+ raise "ERROR: #{target_name} #{packet_name} is read-only and is not supported by #{method_name}()"
576
+ end
577
+ end
578
+
557
579
  def _extract_target_packet_item_names(method_name, *args)
558
580
  target_name = nil
559
581
  packet_name = nil
@@ -589,6 +611,33 @@ module OpenC3
589
611
  end
590
612
 
591
613
  def _tlm_process_args(args, method_name, cache_timeout: nil, scope: $openc3_scope, token: $openc3_token)
614
+ target_name, packet_name, item_name = _tlm_extract_args(args, method_name)
615
+ packet_name = _tlm_resolve_packet_name(target_name, packet_name, item_name, cache_timeout: cache_timeout, scope: scope)
616
+ return [target_name, packet_name, item_name]
617
+ end
618
+
619
+ def _set_tlm_process_args(args, method_name, cache_timeout: nil, scope: $openc3_scope, token: $openc3_token)
620
+ target_name, packet_name, item_name, value = _set_tlm_extract_args(args, method_name)
621
+ _tlm_reject_read_only(target_name, packet_name, method_name)
622
+ packet_name = _tlm_resolve_packet_name(target_name, packet_name, item_name, cache_timeout: cache_timeout, scope: scope)
623
+ return [target_name, packet_name, item_name, value]
624
+ end
625
+
626
+ # Like _tlm_process_args but returns an array of packet names where LATEST
627
+ # expands to every packet containing the item. Used by APIs which change
628
+ # an item so the change applies wherever the item appears.
629
+ def _tlm_process_args_all_packets(args, method_name, scope: $openc3_scope)
630
+ target_name, packet_name, item_name = _tlm_extract_args(args, method_name)
631
+ return [target_name, _tlm_packet_names(target_name, packet_name, item_name, scope: scope), item_name]
632
+ end
633
+
634
+ def _set_tlm_process_args_all_packets(args, method_name, scope: $openc3_scope)
635
+ target_name, packet_name, item_name, value = _set_tlm_extract_args(args, method_name)
636
+ _tlm_reject_read_only(target_name, packet_name, method_name)
637
+ return [target_name, _tlm_packet_names(target_name, packet_name, item_name, scope: scope), item_name, value]
638
+ end
639
+
640
+ def _tlm_extract_args(args, method_name)
592
641
  case args.length
593
642
  when 1
594
643
  target_name, packet_name, item_name = extract_fields_from_tlm_text(args[0])
@@ -600,21 +649,10 @@ module OpenC3
600
649
  # Invalid number of arguments
601
650
  raise "ERROR: Invalid number of arguments (#{args.length}) passed to #{method_name}()"
602
651
  end
603
- target_name = target_name.upcase
604
- packet_name = packet_name.upcase
605
- item_name = item_name.upcase
606
-
607
- if packet_name == 'LATEST'
608
- packet_name = CvtModel.determine_latest_packet_for_item(target_name, item_name, cache_timeout: cache_timeout, scope: scope)
609
- else
610
- # Determine if this item exists, it will raise appropriate errors if not
611
- TargetModel.packet_item(target_name, packet_name, item_name, scope: scope)
612
- end
613
-
614
- return [target_name, packet_name, item_name]
652
+ return [target_name.upcase, packet_name.upcase, item_name.upcase]
615
653
  end
616
654
 
617
- def _set_tlm_process_args(args, method_name, cache_timeout: nil, scope: $openc3_scope, token: $openc3_token)
655
+ def _set_tlm_extract_args(args, method_name)
618
656
  case args.length
619
657
  when 1
620
658
  target_name, packet_name, item_name, value = extract_fields_from_set_tlm_text(args[0])
@@ -627,18 +665,148 @@ module OpenC3
627
665
  # Invalid number of arguments
628
666
  raise "ERROR: Invalid number of arguments (#{args.length}) passed to #{method_name}()"
629
667
  end
630
- target_name = target_name.upcase
631
- packet_name = packet_name.upcase
632
- item_name = item_name.upcase
668
+ return [target_name.upcase, packet_name.upcase, item_name.upcase, value]
669
+ end
633
670
 
671
+ # Resolve LATEST to the most recently received packet containing the item
672
+ def _tlm_resolve_packet_name(target_name, packet_name, item_name, cache_timeout: nil, scope: $openc3_scope)
634
673
  if packet_name == 'LATEST'
635
674
  packet_name = CvtModel.determine_latest_packet_for_item(target_name, item_name, cache_timeout: cache_timeout, scope: scope)
636
675
  else
637
676
  # Determine if this item exists, it will raise appropriate errors if not
638
677
  TargetModel.packet_item(target_name, packet_name, item_name, scope: scope)
639
678
  end
679
+ return packet_name
680
+ end
640
681
 
641
- return [target_name, packet_name, item_name, value]
682
+ VALUE_TYPE_ORDER = %w(RAW CONVERTED FORMATTED).freeze
683
+
684
+ # Returns the value type available for an item given the requested value type
685
+ def _tlm_available_value_type(item, item_name, value_type)
686
+ # Must request the raw value when dealing with the reserved items
687
+ return 'RAW' if Packet::RESERVED_ITEM_NAMES.include?(item_name)
688
+ # Arrays must be accessed as RAW since there's no conversion
689
+ return 'RAW' if item['array_size']
690
+
691
+ # This logic must match the logic in Packet#decom
692
+ converted = (item['states'] or (item['read_conversion'] and item['data_type'] != 'DERIVED'))
693
+ case value_type
694
+ when 'FORMATTED', 'WITH_UNITS'
695
+ return 'FORMATTED' if item['format_string'] or item['units']
696
+ return converted ? 'CONVERTED' : 'RAW'
697
+ when 'CONVERTED'
698
+ return converted ? 'CONVERTED' : 'RAW'
699
+ else # RAW or unknown
700
+ return 'RAW'
701
+ end
702
+ end
703
+
704
+ # Historical lookup where some items are LATEST. Those items have a 6th element
705
+ # holding the item to look up in every packet which contains the item.
706
+ def _tlm_latest_history(cvt_items, start_time:, end_time:, cache_timeout:, scope:)
707
+ latest = cvt_items.each_index.select { |index| cvt_items[index].length == 6 }
708
+ regular = cvt_items.each_index.reject { |index| cvt_items[index].length == 6 }
709
+
710
+ # The QuestDB lookup ASOF JOINs every packet to the rows of the first, so
711
+ # a range needs a regular item first to drive the rows. Without one keep
712
+ # the realtime behavior of using the packet received most recently now.
713
+ if end_time and regular.all? { |index| cvt_items[index][2].nil? }
714
+ cvt_items = cvt_items.map do |target_name, packet_name, item_name, value_type, limits, candidates|
715
+ next [target_name, packet_name, item_name, value_type, limits] unless packet_name == 'LATEST'
716
+ packet_name = CvtModel.determine_latest_packet_for_item(target_name, item_name, cache_timeout: cache_timeout, scope: scope)
717
+ # Use the candidate so the query asks for the value type and limits that packet has
718
+ candidates.find { |candidate| candidate[1] == packet_name } || [target_name, packet_name, item_name, value_type, limits]
719
+ end
720
+ return CvtModel.tsdb_lookup(cvt_items, start_time: start_time, end_time: end_time, scope: scope)
721
+ end
722
+
723
+ # A packet without a table fails the query so skip those candidates
724
+ packets = latest.flat_map { |index| cvt_items[index][5].map { |candidate| candidate[0..1] } }.uniq
725
+ existing = CvtModel.tsdb_packets_with_tables(packets, scope: scope)
726
+ cvt_items = cvt_items.each_with_index.map do |cvt_item, index|
727
+ next cvt_item unless latest.include?(index)
728
+ cvt_item[0..4] + [cvt_item[5].select { |candidate| existing.include?(candidate[0..1]) }]
729
+ end
730
+
731
+ if end_time
732
+ query = []
733
+ query_index = {}
734
+ add = lambda { |query_item| query_index[query_item] ||= (query << query_item; query.length - 1) }
735
+ positions = Array.new(cvt_items.length)
736
+ regular.each { |index| positions[index] = add.call(cvt_items[index]) }
737
+ latest.each do |index|
738
+ positions[index] = cvt_items[index][5].map do |candidate|
739
+ target_name, packet_name = candidate
740
+ [add.call(candidate), add.call([target_name, packet_name, 'PACKET_TIMESECONDS', 'RAW', nil])]
741
+ end
742
+ end
743
+ rows = CvtModel.tsdb_lookup(query, start_time: start_time, end_time: end_time, scope: scope, flatten: false)
744
+ return rows unless rows.is_a?(Array) and rows.length > 0
745
+ rows = rows.map do |row|
746
+ positions.map do |position|
747
+ next row[position] if position.is_a?(Integer)
748
+ # Take the candidate with the newest packet time at this row
749
+ _tlm_newest(position.map { |value_index, time_index| [row[time_index][0], row[value_index]] }) || [nil, nil]
750
+ end
751
+ end
752
+ # Match tsdb_lookup which returns a flat array for a single row
753
+ return rows.length == 1 ? rows[0] : rows
754
+ end
755
+
756
+ # A point in time returns the last row of the first packet before start_time
757
+ # with the other packets ASOF JOINed at that row. A candidate packet received
758
+ # after that row would be missed so query each candidate packet on its own.
759
+ found = false
760
+ row = Array.new(cvt_items.length) { [nil, nil] }
761
+ unless regular.all? { |index| cvt_items[index][2].nil? }
762
+ result = CvtModel.tsdb_lookup(regular.map { |index| cvt_items[index] }, start_time: start_time, scope: scope, flatten: false)
763
+ if result.is_a?(Array) and result.length > 0
764
+ found = true
765
+ regular.each_with_index { |index, result_index| row[index] = result[0][result_index] }
766
+ end
767
+ end
768
+ # Group the LATEST items by candidate packet so each packet is queried once
769
+ by_packet = {}
770
+ latest.each do |index|
771
+ cvt_items[index][5].each do |candidate|
772
+ target_name, packet_name = candidate
773
+ (by_packet[[target_name, packet_name]] ||= {})[index] = candidate
774
+ end
775
+ end
776
+ found_values = Hash.new { |hash, key| hash[key] = [] }
777
+ by_packet.each do |(target_name, packet_name), packet_items|
778
+ query = packet_items.values.uniq
779
+ query << [target_name, packet_name, 'PACKET_TIMESECONDS', 'RAW', nil]
780
+ result = CvtModel.tsdb_lookup(query, start_time: start_time, scope: scope, flatten: false)
781
+ next unless result.is_a?(Array) and result.length > 0
782
+ values = result[0]
783
+ packet_items.each { |index, query_item| found_values[index] << [values[-1][0], values[query.index(query_item)]] }
784
+ end
785
+ found_values.each do |index, candidates|
786
+ value = _tlm_newest(candidates)
787
+ next unless value
788
+ found = true
789
+ row[index] = value
790
+ end
791
+ return found ? row : {}
792
+ end
793
+
794
+ # Return the value with the newest time from [time, value] pairs, or nil
795
+ def _tlm_newest(candidates)
796
+ candidates.select { |time, _| time }.max_by { |time, _| time }&.last
797
+ end
798
+
799
+ # Expand LATEST to every packet containing the item
800
+ def _tlm_packet_names(target_name, packet_name, item_name, scope: $openc3_scope)
801
+ if packet_name == 'LATEST'
802
+ packet_names = TargetModel.get_item_to_packet_map(target_name, scope: scope)[item_name]
803
+ raise "Item '#{target_name} LATEST #{item_name}' does not exist for scope: #{scope}" unless packet_names
804
+ return packet_names
805
+ else
806
+ # Determine if this item exists, it will raise appropriate errors if not
807
+ TargetModel.packet_item(target_name, packet_name, item_name, scope: scope)
808
+ return [packet_name]
809
+ end
642
810
  end
643
811
  end
644
812
  end
@@ -14,7 +14,6 @@
14
14
  require 'openc3/interfaces/interface'
15
15
  require 'openc3/config/config_parser'
16
16
  require 'faraday'
17
- require 'faraday/follow_redirects'
18
17
  require 'openc3/accessors/http_accessor'
19
18
 
20
19
  # TODO: Header Log Filtering? File Uploads? Authorization from secrets?
@@ -35,6 +34,7 @@ module OpenC3
35
34
  else
36
35
  @url = "#{@protocol}://#{@hostname}:#{@port}"
37
36
  end
37
+ @base_uri = URI(@url)
38
38
  @write_timeout = ConfigParser.handle_nil(write_timeout)
39
39
  @write_timeout = Float(@write_timeout) if @write_timeout
40
40
  @read_timeout = ConfigParser.handle_nil(read_timeout)
@@ -64,8 +64,9 @@ module OpenC3
64
64
  request['open_timeout'] = @connect_timeout if @connect_timeout
65
65
  request['read_timeout'] = @read_timeout if @read_timeout
66
66
  request['write_timeout'] = @write_timeout if @write_timeout
67
+ # Redirects are not followed, because a redirect to another host would bypass
68
+ # validate_uri. A 3xx response is returned like any other response.
67
69
  @http = Faraday.new(request: request) do |f|
68
- f.response :follow_redirects # use Faraday::FollowRedirects::Middleware
69
70
  f.adapter :net_http # adds the adapter to the connection, defaults to `Faraday.default_adapter`
70
71
  end
71
72
  super()
@@ -108,7 +109,7 @@ module OpenC3
108
109
  extra = packet.extra
109
110
  extra ||= {}
110
111
  data = packet.buffer(true) # Copy buffer so logged command isn't modified
111
- extra['HTTP_URI'] = URI("#{@url}#{packet.read('HTTP_PATH')}").to_s
112
+ extra['HTTP_URI'] = build_uri(packet.read('HTTP_PATH'))
112
113
  # Store the target name for use in identifying the response
113
114
  extra['HTTP_REQUEST_TARGET_NAME'] = packet.target_name
114
115
  return data, extra
@@ -123,33 +124,43 @@ module OpenC3
123
124
  headers ||= {}
124
125
  uri = extra['HTTP_URI']
125
126
  method = extra['HTTP_METHOD']
127
+ # HTTP_URI normally comes from convert_packet_to_data, but write_raw passes
128
+ # extra straight through, so check the destination here as well
129
+ validate_uri(uri)
126
130
 
127
131
  resp = nil
128
- case method
129
- when 'get'
130
- resp = @http.get(uri) do |req|
131
- req.params = queries
132
- req.headers = headers
133
- end
134
- when 'put'
135
- resp = @http.put(uri) do |req|
136
- req.params = queries
137
- req.headers = headers
138
- req.body = data
139
- end
140
- when 'delete'
141
- resp = @http.delete(uri) do |req|
142
- req.params = queries
143
- req.headers = headers
144
- end
145
- when 'post'
146
- resp = @http.post(uri) do |req|
147
- req.params = queries
148
- req.headers = headers
149
- req.body = data
132
+ begin
133
+ case method
134
+ when 'get'
135
+ resp = @http.get(uri) do |req|
136
+ req.params = queries
137
+ req.headers = headers
138
+ end
139
+ when 'put'
140
+ resp = @http.put(uri) do |req|
141
+ req.params = queries
142
+ req.headers = headers
143
+ req.body = data
144
+ end
145
+ when 'delete'
146
+ resp = @http.delete(uri) do |req|
147
+ req.params = queries
148
+ req.headers = headers
149
+ end
150
+ when 'post'
151
+ resp = @http.post(uri) do |req|
152
+ req.params = queries
153
+ req.headers = headers
154
+ req.body = data
155
+ end
156
+ else
157
+ raise "Unsupported HTTP Method: #{method}"
150
158
  end
151
- else
152
- raise "Unsupported HTTP Method: #{method}"
159
+ rescue Faraday::Error => e
160
+ # Log the details but don't return the raw error to the command sender
161
+ # Query strings can carry credentials, so they are redacted from the log
162
+ Logger.error("#{@name}: HTTP #{method.to_s.upcase} #{redact_queries(uri, uri: true)} failed: #{e.class}: #{redact_queries(e.message)}")
163
+ raise "HTTP #{method.to_s.upcase} request to #{@url} failed (#{e.class.name.split('::').last})"
153
164
  end
154
165
 
155
166
  # Normalize Response into simple hash
@@ -248,5 +259,51 @@ module OpenC3
248
259
  result['response_queue_length'] = @response_queue.length
249
260
  return result
250
261
  end
262
+
263
+ protected
264
+
265
+ # Builds the request URI from the configured URL and the HTTP_PATH parameter.
266
+ # The request always goes to the configured scheme, host and port.
267
+ #
268
+ # @param path [String] Request path, which must start with a single '/'
269
+ # @return [String] Full request URI
270
+ def build_uri(path)
271
+ path = path.to_s
272
+ unless path.empty?
273
+ if !path.start_with?('/') or path.start_with?('//')
274
+ raise WriteRejectError, "HTTP_PATH must start with a single '/': #{path.inspect}"
275
+ end
276
+ if path.match?(/[\\ [:cntrl:]]/)
277
+ raise WriteRejectError, "HTTP_PATH contains invalid characters: #{path.inspect}"
278
+ end
279
+ end
280
+ uri = "#{@url}#{path}"
281
+ validate_uri(uri)
282
+ return uri
283
+ end
284
+
285
+ # Replaces query strings with ?[REDACTED] so they aren't logged. In a URI everything
286
+ # after the '?' is the query. In other text the query ends at whitespace, which
287
+ # can't appear in a query because HTTP_PATH rejects it and queries are percent-encoded.
288
+ def redact_queries(text, uri: false)
289
+ return text.to_s.sub(/\?.*/m, '?[REDACTED]') if uri
290
+ return text.to_s.gsub(/\?\S+/, '?[REDACTED]')
291
+ end
292
+
293
+ # Raises WriteRejectError unless the URI goes to the configured scheme, host and port.
294
+ # WriteRejectError rejects the command without disconnecting the interface.
295
+ def validate_uri(uri)
296
+ begin
297
+ parsed = URI(uri.to_s)
298
+ rescue URI::InvalidURIError
299
+ raise WriteRejectError, "Invalid HTTP URI: #{uri.inspect}"
300
+ end
301
+ unless parsed.scheme.to_s.casecmp?(@base_uri.scheme) and
302
+ parsed.host.to_s.casecmp?(@base_uri.host) and
303
+ parsed.port == @base_uri.port and
304
+ parsed.userinfo.nil?
305
+ raise WriteRejectError, "HTTP URI #{uri.inspect} does not match interface URL #{@url}"
306
+ end
307
+ end
251
308
  end
252
309
  end