fix: expose IMS call failures and allow unlimited calls

This commit is contained in:
MengMengCode
2026-08-09 21:52:25 +08:00
parent 337aa3c0ab
commit d8828ff26a
5 changed files with 110 additions and 24 deletions
+9 -5
View File
@@ -76,8 +76,8 @@ func (s *Server) handleCallAction(w http.ResponseWriter, r *http.Request, config
return true return true
} }
duration = time.Duration(request.DurationSeconds) * time.Second duration = time.Duration(request.DurationSeconds) * time.Second
if duration < time.Second || duration > maxCallDuration { if duration < 0 || duration > maxCallDuration {
writeError(w, http.StatusBadRequest, "invalid_duration", "duration_seconds must be between 1 and 600") writeError(w, http.StatusBadRequest, "invalid_duration", "duration_seconds must be 0 (no automatic hang-up) or between 1 and 600")
return true return true
} }
command = "ATD" + number + ";" command = "ATD" + number + ";"
@@ -137,7 +137,9 @@ func (s *Server) handleCallAction(w http.ResponseWriter, r *http.Request, config
if call, ok := result.(vowifi.Call); ok { if call, ok := result.(vowifi.Call); ok {
callID = call.ID callID = call.ID
} }
go s.hangupVoWiFiAfter(config.ID, callID, duration) if duration > 0 {
go s.hangupVoWiFiAfter(config.ID, callID, duration)
}
} }
s.recordAudit(r.Context(), "admin", "call."+action, "device", config.ID, "success", transport) s.recordAudit(r.Context(), "admin", "call."+action, "device", config.ID, "success", transport)
writeJSON(w, http.StatusAccepted, map[string]any{"data": map[string]any{ writeJSON(w, http.StatusAccepted, map[string]any{"data": map[string]any{
@@ -158,7 +160,9 @@ func (s *Server) handleCallAction(w http.ResponseWriter, r *http.Request, config
return true return true
} }
if action == "dial" { if action == "dial" {
go s.hangupAfter(config.ID, physicalID, duration) if duration > 0 {
go s.hangupAfter(config.ID, physicalID, duration)
}
} }
s.recordAudit(r.Context(), "admin", "call."+action, "device", config.ID, "success", transport) s.recordAudit(r.Context(), "admin", "call."+action, "device", config.ID, "success", transport)
writeJSON(w, http.StatusAccepted, map[string]any{ writeJSON(w, http.StatusAccepted, map[string]any{
@@ -179,7 +183,7 @@ func resolveVoWiFiCallID(controller VoWiFiCallController, deviceID, id, required
return "", err return "", err
} }
for _, call := range calls { for _, call := range calls {
if requiredState == "" || call.State == requiredState { if call.State != "ended" && call.State != "failed" && (requiredState == "" || call.State == requiredState) {
return call.ID, nil return call.ID, nil
} }
} }
+30
View File
@@ -1,6 +1,7 @@
package server package server
import ( import (
"context"
"testing" "testing"
"vocat/internal/modem" "vocat/internal/modem"
@@ -41,3 +42,32 @@ func TestCallTransportRequiresIMSReady(t *testing.T) {
t.Fatalf("callTransport with IMS ready = %q, want vowifi", got) t.Fatalf("callTransport with IMS ready = %q, want vowifi", got)
} }
} }
func TestResolveVoWiFiCallIDIgnoresTerminalCalls(t *testing.T) {
controller := &fakeCallController{calls: []vowifi.Call{
{ID: "failed", State: "failed"},
{ID: "active", State: "active"},
}}
got, err := resolveVoWiFiCallID(controller, "ec20", "", "")
if err != nil || got != "active" {
t.Fatalf("resolveVoWiFiCallID() = %q, %v; want active", got, err)
}
}
type fakeCallController struct {
calls []vowifi.Call
}
func (controller *fakeCallController) Calls(string) ([]vowifi.Call, error) {
return controller.calls, nil
}
func (*fakeCallController) DialCall(context.Context, string, string) (vowifi.Call, error) {
return vowifi.Call{}, nil
}
func (*fakeCallController) AnswerCall(context.Context, string, string) (vowifi.Call, error) {
return vowifi.Call{}, nil
}
func (*fakeCallController) HangupCall(context.Context, string, string) error { return nil }
+46 -12
View File
@@ -18,6 +18,8 @@ var (
ErrCallState = errors.New("ims: call is not in the required state") ErrCallState = errors.New("ims: call is not in the required state")
) )
const terminalCallRetention = 30 * time.Second
type imsCall struct { type imsCall struct {
public vowifi.Call public vowifi.Call
callID string callID string
@@ -37,11 +39,14 @@ type imsCall struct {
func (session *Session) Calls() []vowifi.Call { func (session *Session) Calls() []vowifi.Call {
session.callMu.Lock() session.callMu.Lock()
defer session.callMu.Unlock() defer session.callMu.Unlock()
now := time.Now().UTC()
calls := make([]vowifi.Call, 0, len(session.calls)) calls := make([]vowifi.Call, 0, len(session.calls))
for _, call := range session.calls { for id, call := range session.calls {
if call.public.State != "ended" && call.public.State != "failed" { if call.public.EndedAt != nil && now.Sub(*call.public.EndedAt) > terminalCallRetention {
calls = append(calls, call.public) delete(session.calls, id)
continue
} }
calls = append(calls, call.public)
} }
sort.Slice(calls, func(i, j int) bool { return calls[i].StartedAt.Before(calls[j].StartedAt) }) sort.Slice(calls, func(i, j int) bool { return calls[i].StartedAt.Before(calls[j].StartedAt) })
return calls return calls
@@ -144,13 +149,14 @@ func (session *Session) watchOutgoingCall(call *imsCall, key sipTransactionKey)
case <-session.refreshContext.Done(): case <-session.refreshContext.Done():
return return
case <-timer.C: case <-timer.C:
session.setCallState(call.callID, "failed") session.finishCall(call.callID, "failed", 0, "SIP INVITE transaction timed out")
return return
case response := <-call.responses: case response := <-call.responses:
if response == nil { if response == nil {
continue continue
} }
if response.StatusCode < 200 { if response.StatusCode < 200 {
session.setCallDiagnostic(call.callID, response.StatusCode, response.Reason)
if response.StatusCode >= 180 { if response.StatusCode >= 180 {
session.setCallState(call.callID, "ringing") session.setCallState(call.callID, "ringing")
} }
@@ -170,7 +176,7 @@ func (session *Session) watchOutgoingCall(call *imsCall, key sipTransactionKey)
_ = session.sendACK(call) _ = session.sendACK(call)
session.setCallState(call.callID, "active") session.setCallState(call.callID, "active")
} else { } else {
session.setCallState(call.callID, "failed") session.finishCall(call.callID, "failed", response.StatusCode, response.Reason)
} }
return return
} }
@@ -223,18 +229,18 @@ func (session *Session) HangupCall(ctx context.Context, id string) error {
if err := respond(response); err != nil { if err := respond(response); err != nil {
return err return err
} }
session.setCallState(id, "ended") session.finishCall(id, "ended", 0, "")
return nil return nil
} }
method := "BYE" method := "BYE"
if direction == "outgoing" && (state == "dialing" || state == "ringing") { if direction == "outgoing" && (state == "dialing" || state == "ringing") {
method = "CANCEL" method = "CANCEL"
} }
if err := session.sendDialogRequest(ctx, call, method); err != nil { err := session.sendDialogRequest(ctx, call, method)
return err // A remote endpoint may already have removed the dialog and answer BYE with
} // 481. The local call must still leave the active list after a hang-up.
session.setCallState(id, "ended") session.finishCall(id, "ended", 0, "")
return nil return err
} }
func (session *Session) handleCallRequest(request *sipRequest, respond func([]byte) error) bool { func (session *Session) handleCallRequest(request *sipRequest, respond func([]byte) error) bool {
@@ -280,7 +286,7 @@ func (session *Session) handleCallRequest(request *sipRequest, respond func([]by
} }
} }
} }
session.setCallState(callID, "ended") session.finishCall(callID, "ended", 0, "")
return true return true
default: default:
return false return false
@@ -395,6 +401,34 @@ func (session *Session) setCallState(id, state string) {
session.callMu.Lock() session.callMu.Lock()
if call := session.calls[id]; call != nil { if call := session.calls[id]; call != nil {
call.public.State = state call.public.State = state
if state != "ended" && state != "failed" {
call.public.EndedAt = nil
}
}
session.callMu.Unlock()
}
func (session *Session) setCallDiagnostic(id string, code int, reason string) {
session.callMu.Lock()
if call := session.calls[id]; call != nil {
call.public.SIPCode = code
call.public.Reason = safeSIPDiagnostic(reason)
}
session.callMu.Unlock()
}
func (session *Session) finishCall(id, state string, code int, reason string) {
now := time.Now().UTC()
session.callMu.Lock()
if call := session.calls[id]; call != nil {
call.public.State = state
if code != 0 {
call.public.SIPCode = code
}
if reason = safeSIPDiagnostic(reason); reason != "" {
call.public.Reason = reason
}
call.public.EndedAt = &now
} }
session.callMu.Unlock() session.callMu.Unlock()
} }
+17 -2
View File
@@ -4,6 +4,8 @@ import (
"context" "context"
"strings" "strings"
"testing" "testing"
"vocat/internal/vowifi"
) )
func TestIncomingCallCanRingAndAnswerWithoutAudio(t *testing.T) { func TestIncomingCallCanRingAndAnswerWithoutAudio(t *testing.T) {
@@ -60,8 +62,21 @@ func TestIncomingCallCanBeRejected(t *testing.T) {
if !strings.HasPrefix(string(response), "SIP/2.0 486 Busy Here") { if !strings.HasPrefix(string(response), "SIP/2.0 486 Busy Here") {
t.Fatalf("reject response = %q", response) t.Fatalf("reject response = %q", response)
} }
if len(session.Calls()) != 0 { calls := session.Calls()
t.Fatal("ended call remained active") if len(calls) != 1 || calls[0].State != "ended" || calls[0].EndedAt == nil {
t.Fatalf("terminal call status = %#v", calls)
}
}
func TestRejectedOutgoingCallRetainsSIPReason(t *testing.T) {
session := &Session{calls: make(map[string]*imsCall)}
call := &imsCall{public: vowifi.Call{ID: "rejected", State: "dialing"}}
session.calls[call.public.ID] = call
session.finishCall(call.public.ID, "failed", 484, "Address Incomplete\r\nignored")
calls := session.Calls()
if len(calls) != 1 || calls[0].State != "failed" || calls[0].SIPCode != 484 ||
calls[0].Reason != "Address Incomplete ignored" || calls[0].EndedAt == nil {
t.Fatalf("rejected call = %#v", calls)
} }
} }
+8 -5
View File
@@ -368,11 +368,14 @@ type SMSSender interface {
// Call describes one signalling-only IMS call. VoCat intentionally does not // Call describes one signalling-only IMS call. VoCat intentionally does not
// open, capture, or relay an RTP media stream for extension call tests. // open, capture, or relay an RTP media stream for extension call tests.
type Call struct { type Call struct {
ID string `json:"id"` ID string `json:"id"`
Number string `json:"number"` Number string `json:"number"`
Direction string `json:"direction"` Direction string `json:"direction"`
State string `json:"state"` State string `json:"state"`
StartedAt time.Time `json:"started_at"` StartedAt time.Time `json:"started_at"`
SIPCode int `json:"sip_code,omitempty"`
Reason string `json:"reason,omitempty"`
EndedAt *time.Time `json:"ended_at,omitempty"`
} }
// CallController is an optional capability of an IMS session. Implementations // CallController is an optional capability of an IMS session. Implementations